Can we change it to match on multiple attributes like this?
/**
* Finds the first child that has the attributes with the specified values.
* The function accepts multiple name/value pairs.
* @param {String} attribute The attribute name
* @param {Mixed} value The value to search for
* @return {Node} The found child or null if none was found
*/
findChild : function(){
var cs = this.childNodes;
for(var i = 0, len = cs.length; i < len; i++) {
var match = true;
for (var j = 0, alen = arguments.length; match && j < alen; j += 2) {
if(cs[i].attributes[arguments[j]] != arguments[j + 1]){
match = false;
}
}
if (match) {
return cs[i];
}
}
return null;
},

|
#2
|
|
I think it's better to stick with the Ext standard and just offer a power findBy:
/**
* Finds the first child by a custom function. The child matches if the function passed
* returns true.
* @param {Function} fn
* @param {Object} scope (optional)
* @return {Node} The found child or null if none was found
*/
findChildBy : function(fn, scope){
var cs = this.childNodes;
for(var i = 0, len = cs.length; i < len; i++) {
if(fn.call(scope||cs[i], cs[i]) === true){
return cs[i];
}
}
return null;
},
![]() |
|
#3
|
|
Excellent solution, thanks!
![]() |
本文介绍了一种在DOM中查找具有特定属性值的子元素的方法。提供了两种实现方式:一种是通过指定多个属性名和值进行匹配;另一种是使用自定义函数进行灵活匹配。这两种方法都提高了查找子元素的灵活性和效率。
319

被折叠的 条评论
为什么被折叠?



