1. 获取元素: const cucumber = document.querySelector('span:nth-of-type(1)'); const tomato = document.querySelector('span:nth-of-type(2)'); const total = document.querySelector('span:nth-of-type(3)');

  2. 获取元素内容: const cucumberPrice = cucumber.innerHTML; const tomatoPrice = tomato.innerHTML;

  3. 使用 parseInt 获取整数值: const cucumberPriceInt = parseInt(cucumberPrice); const tomatoPriceInt = parseInt(tomatoPrice);

  4. 使用 parseFloat 获取浮点值: const cucumberPriceFloat = parseFloat(cucumberPrice); const tomatoPriceFloat = parseFloat(tomatoPrice);

  5. 计算总价: const totalCost = 2 * cucumberPriceFloat + 3 * tomatoPriceFloat;

  6. 将结果放入页面的总价标签中: total.innerHTML = totalCost.toFixed(2); // 保留两位小

span35斤黄瓜spanspan27斤西红柿spanspan元span功能要求1从页面中获取 span 标签使用样式选择器2正确获取元素内容 innerHTML3正确使用 parseint 获取整数值4正确是 parseFloat 获取浮点值5正确计算 2 斤黄瓜 3 斤西红柿的总价6将结果使用 innerHTML 放入页面的 span 总价标签中

原文地址: https://www.cveoy.top/t/topic/cHqY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录