var walk_the_DOM = function walk(node, func){
func(node);
node = node.firstChild;
while (node) {
walk(node,func);
node = node.nextSibling;
}
};
var getElementsByAttribute = function (att, value) {
var results = [];
walk_the_DOM(document.body,function (node) {
var actual = node.nodeType === 1 && node.getAttribute(att);
if (typeof actual === "string" && (actual === value || typeof value !== "string")) {
results.push(node);
}
});
return results;
};
主要是if (typeof actual === "string" && (actual === value || typeof value !== "string"))这一句
圈住的那一部分
这个“actual ” 是上句获取到的属性值;进而来和value 来做对比;当值一样是也就是
这个条件成立;证明这个是要找的值,这个是找到属性和属性值一样的元素;
这个条件成立时;是找到含有这个属性;但属性值不相同的元素;(从字面上理解)
总结:就是这个通过属性获取元素的方法,必须需要一个属性,属性值非必须;