为什么这段实现数量增加的代码运行提示Cannot read property 'value' of undefined"

//script

var addd = document.getElementsByClassName("add");

for(var n=0;n<addd.length;n++){

addd[n].onclick=function(){

var j = document.getElementsByClassName("count-input");

var h = parseInt(j[n].value)

j[n].value=h+1;

}

//html

<tr><td>

<input class="count-input" value="1"/>

<span class="add">+</span>

</td></tr>

<tr><td>

<input class="count-input" value="1"/>

<span class="add">+</span>

</td></tr>


79351
浏览 2305回答 4
4回答

QQ_随意

作用域问题(function(n){addd[n].onclick=function(){    var j = document.getElementsByClassName("count-input");    var h = parseInt(j[n].value)    j[n].value=h+1;}})(n)加个闭包就行了

慕田峪6141110

NJMRRZMZJWKZCGMCWYHHSIPIIVEIDGJUBCOHXUKBZTJGUVEIFCDJMTHMPYFVZTJTDDMFRTMNMTFTCSSSNRDNQURSANQAUIJJPVCGVICMCTMTJMHSCJWXXHBKONMATYOXNTWDWLVVNDAGKQSZVEURPVVPHKBEDQWJZMGQXRXTQXDKRLSSFXDDJPCLFSFXPZPAXQWZJJJVERUBDWJGMTFMPGNDNADHSLBHJZPRYVHWWPZCCZJCGVIRLBXUNXHRXMCTCNQUNAKOJKFZFCTGFWWWCCSCQERTRDLDMDQXBYHBRCONUJWRXANDRXAXXAXTDGXGLJZVVVJLFCWQXNEUOKKNUEQNZPZPFLSOJYBOXTPCZJZIUGRHANKJNAJZYFULEYVVYBTQUGMWSZFZYFBCMMSCESBMFIBRSHXURXPRPOVTTDWGDLXXQNUSWCDCCSFWDGGWVPMCJTAEAKJQGZMGXKDKGAQNQLRQGGJHHDAGUJWQGVRKAKTKJFFLLBBEVUWFKXXXEZFPLMCOEMCFVPYRUNXNWAODXTCKWMVBLBVYCYDPCFOQKJMDXQIWWMNKYESRZBBEKAYXHUATFBVVSIWFQQDEOOHAGGZTAJZJZMSWNKXDJMOEXXDQBLPTFIOOOGWIZIVKXATTIBUBUPLQHNNGJZGKHGVPSBKXMWZZRHBDEICFCMENJQTMQZIANDTMUAKMFVFDBALENUKLRYBWSTJAQAQMVFQNWAJYHXYCYJKUIGCFUKAQVYSSDPVLHEXKZORHENPMPMNZFPPOELHEHKYAAQVEOLLEJFCIMZILURYRHJPPMGSVFUEHJCPWZDTXGOKEOLVCVIOBGCYIMJGCYIXJROLCYCEOJGPFIRODLBWTCQLHDTPFOEVE

79351

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .count{ width: 90px; } .reduce{ width: 17px; height: 23px; background-color: gainsboro; float: left; border: 1px solid #e5e5e5; line-height: 23px; } .count-input{ width: 50px; height: 15px; float: left; text-align: center; border: 1px solid #aaa; color: #343434; padding: 4px 0; } .add{ width: 17px; height: 23px; background-color: gainsboro; float: left; text-align: center; line-height: 23px; border: 1px solid #e5e5e5; } </style> </head> <body> <table> <tr > <td class="count"> <span class="reduce"></span> <input class="count-input" value="1"/> <span class="add">+</span> </td> </tr> <tr > <td class="count"> <span class="reduce"></span> <input class="count-input" value="1"/> <span class="add">+</span> </td> </tr> </table> <script type="text/javascript"> var addd = document.getElementsByClassName("add"); for(var n=0;n<addd.length;n++){ addd[n].onclick=function(){ var j = document.getElementsByClassName("count-input"); var h = parseInt(j[n].value) j[n].value=h+1; }} </script> </body></html>

qq_佛说过过过_0

我这边并没有报错,你把代码贴全了,我运行看看。

慕粉3166282

没有那个属性
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript