js 中的for in 与标准for循环遍历数组
来源:互联网 发布:优化机构设置城管局 编辑:程序博客网 时间:2024/04/29 04:52
<script type="text/javascript">
function checkAll(node){
var item = document.getElementsByName("item");
/*alert(item.length);*/
for(i in item){
item[i].checked = node.checked;
}
}
function getSum(){
var item = document.getElementsByName("item");
alert(item);
var sum = 0;
var s = 0;
for(var i in item)
/* for(var i = 0;i<item.length;i++) 可以正常显示*/
{ alert(typeof(i)+":"+i);/*String类型的,0,1,2,3,4,5,length,item,最后的结果为NaN*/
if(item[i].checked)
{
s = parseInt(item[i].value);
sum += s;
}
}
alert("sum:"+sum);
var str = sum +"元";
document.getElementById("sumid").innerHTML = str;
/*document.getElementById("sumid").innerHTML = str.fontcolor('red');*/
}
</script>
<body>
<div>商品列表</div>
<input type="checkbox" name="all" onclick="checkAll(this)" /> 全选<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="all" onclick="checkAll(this)" /> 全选<br />
<input type="button" value="总金额:" onclick="getSum()" /><span id="sumid"></span>
function checkAll(node){
var item = document.getElementsByName("item");
/*alert(item.length);*/
for(i in item){
item[i].checked = node.checked;
}
}
function getSum(){
var item = document.getElementsByName("item");
alert(item);
var sum = 0;
var s = 0;
for(var i in item)
/* for(var i = 0;i<item.length;i++) 可以正常显示*/
{ alert(typeof(i)+":"+i);/*String类型的,0,1,2,3,4,5,length,item,最后的结果为NaN*/
if(item[i].checked)
{
s = parseInt(item[i].value);
sum += s;
}
}
alert("sum:"+sum);
var str = sum +"元";
document.getElementById("sumid").innerHTML = str;
/*document.getElementById("sumid").innerHTML = str.fontcolor('red');*/
}
</script>
<body>
<div>商品列表</div>
<input type="checkbox" name="all" onclick="checkAll(this)" /> 全选<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="item" value="3000" />笔记本电脑3000元<br />
<input type="checkbox" name="all" onclick="checkAll(this)" /> 全选<br />
<input type="button" value="总金额:" onclick="getSum()" /><span id="sumid"></span>
</body>
建议遍历循环数组时,用标准的for循环
0 0
- js 中的for in 与标准for循环遍历数组
- JS数组的遍历方式for循环与for...in
- js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)
- js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)
- js数组遍历for与for in区别
- for循环与for...in循环JS
- js中的in-for循环
- for-in循环和for循环遍历数组
- for in 循环遍历对象和数组
- js中的循环遍历数组中的元素,ES6(for-of)、ES5(forEach、for-in)、通用(for(i=0;i<length;i++))
- js中使用for 循环和 for in 遍历数组区别
- js 使用for循环遍历数组
- js中标准for循环与foreach(for in)的区别
- for 循环遍历数组
- JS中for 循环 与 for in 循环的区别
- for...in遍历数组
- js 循环 for in
- JS for-in 循环
- cassandra安装
- Linux Vxlan网络隧道互通环境模拟
- oracle to_date
- Oracle Linux 6 安装telnet包问题处理
- Android学习篇:Fragment介绍
- js 中的for in 与标准for循环遍历数组
- UITablerView(表视图基础)
- 访问系统的相册
- 彻底解决:Keil编译提示“File has been changed outside the editor, reload?”提示!
- Areas on the Cross-Section Diagram
- java读取excel 支持所有版本
- [CFF认证]201509-3模板生成系统(C++)
- yii-basic(一)安装及简介
- 第十四周项目2-二叉树排序树中查找的路径