我想查找特定 div 内具有类行的所有 div 的计数
<div class="input_fields_wrap">
<div class="row"></div>
<div class="row"></div>
</div>
我尝试过这个,但它不起作用......
var div = document.getElementByClassName('input_field_wrap');
var myrows = div.getElementsByClassName('row');
alert(myrows.length);
最佳答案
使用document#querySelectorAll
,并检查作为父选择器子级的行:
var numOfRows = document.querySelectorAll('.input_fields_wrap > .row').length;
console.log(numOfRows);
<div class="input_fields_wrap">
<div class="row"></div>
<div class="row"></div>
</div>
为什么您的代码不起作用:
//var div = document.getElementByClassName('input_field_wrap');
var div = document.getElementsByClassName('input_fields_wrap'); // typo in getElementsByClassName - missing s in Elements
//var myrows = div.getElementsByClassName('row');
var myrows = div[0].getElementsByClassName('row'); // div is an array like object, and you want to get the 1st item
alert(myrows.length);
<div class="input_fields_wrap">
<div class="row"></div>
<div class="row"></div>
</div>
关于javascript - 查找并计算特定 div 内的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41096386/