【问题标题】:Xui.js .children() alternativeXui.js .children() 替代
【发布时间】:2012-05-16 16:36:50
【问题描述】:

jQuery 的.children() 的 xui.js(或纯 javascript,如有必要)替代品是什么?

更具体地说,我正在尝试获取 $x(this) 的孩子,但 $x(this).children('p') 不起作用(因为它似乎不存在于库中。根据 Firebug 或是否在 xuijs 文档中)。

【问题讨论】:

  • “不起作用”提供的信息很少。解释问题是如何表现出来的。
  • 如中,它不存在。文档显示知道这样的功能,并且萤火虫通过不识别该功能来确认它。我不认为它是图书馆的一部分。

标签: javascript jquery xui


【解决方案1】:

不确定那个库,但如果你有原生 DOM 元素,原生 JS 可能看起来像这样:

[].filter.call(this.children, function(el, i) { 
    return el.nodeName.toLowerCase() === 'p'; 
});

如果您支持旧版浏览器,您可以从 MDN 获得 Array.prototype.filter 的 shim。

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter#Compatibility


你也可以轻松地制作一个可重用的函数。

function children(elem, s) {
    return [].filter.call(elem.children, function(el, i) { 
        return el.nodeType === 1 && (!s || el.nodeName.toLowerCase() === s); 
    });
}

children(this, 'p');

它仅按标签名称过滤,但扩展它会很简单。

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 2022-10-25
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多