TreeNode.findChild - Ext JS

本文介绍了一种在DOM中查找具有特定属性值的子元素的方法。提供了两种实现方式:一种是通过指定多个属性名和值进行匹配;另一种是使用自定义函数进行灵活匹配。这两种方法都提高了查找子元素的灵活性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    },
Reply With Quote
  #2  
Old 03-09-2007, 04:54 PM
Default

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;
    },
Is that an acceptable alternative?
Reply With Quote
  #3  
Old 03-10-2007, 02:03 AM
Default

Excellent solution, thanks!
Reply With Quote
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值