<!DOCTYPE html><html><head><meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Page Title</title><meta name="viewport" content="width=device-width, initial-scale=1"><script>window.onload = function() {var len1 = getByClass('on', 'list');alert(len1.length); // 结果等于2为正确var len2 = getByClass('on');alert(len2.length); // 结果等于3为正确}function getByClass(clsName, parent) {//定义函数getByClassvar oP = parent ? document.getElementById(parent) : document;//是一个判断语句,如果传入parent即parent存在,那么var op = document.getElementById(parent),不存在的话就是var op = documentvar a = oP.getElementsByTagName('*');var b = new Array();for (var i = 0; i < a.length; i++) {if (a[i].className == clsName) {b.push(a[i]);}return b;}}</script></head><body><ul id="list"><li class="on">1</li><li class="select">2</li><li class="on">3</li><li>4</li></ul><p class="on">class为on的p标签</p></body></html>
添加回答
举报
0/150
提交
取消