這個為什么只輸出了一個hobby1呢?大佬們求看
<!DOCTYPE HTML>
<html>
? ? <head>
? ? ? ? <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
? ? ? ? <title>無標(biāo)題文檔</title>
? ? </head>
? ??
? ? <body>
? ? ? ? <form>
請選擇你愛好:<br>
<input type="checkbox" name="hobby" id="hobby1">? 音樂
<input type="checkbox" name="hobby" id="hobby2">? 登山
<input type="checkbox" name="hobby" id="hobby3">? 游泳
<input type="checkbox" name="hobby" id="hobby4">? 閱讀
<input type="checkbox" name="hobby" id="hobby5">? 打球
<input type="checkbox" name="hobby" id="hobby6">? 跑步 <br>
<input name="get" type="button" value="獲取hobby的id屬性值" onclick = "getid();">
? ? ? ? </form>
? ? ? ? <script type="text/javascript">
function getid(){
var con=document.getElementsByName("hobby");
var i=0;
while(i<con.length){
? ? var text=con[i].getAttribute("id");
document.write(text+"<br>");
i++;
}
}
? ? ? ? </script>
? ? </body>
</html>
2018-06-13
循環(huán)里是可以寫document.write()的?。。】赡苁且?yàn)閛nclick 點(diǎn)擊事件 點(diǎn)擊一下事件發(fā)生后,做不了循環(huán);
2018-06-07
我就是想問為什么這里不能輸出,循環(huán)里面可以寫document.write()函數(shù)吧?
2018-06-07
循環(huán)里面不能寫document.write()這個函數(shù),不信你可以把document.write()換成alert()試一下。。然后text變量要先定義成var text=“” 然后用 ? text += con[i].getAttribute("id");