如何使用 Prototype 复制 YUI 的 getElementsBy?

发布于 2024-08-26 11:30:45 字数 167 浏览 4 评论 0原文

我正在将一些代码从 YUI 移动到 javascript,其中一些代码使用 YUI 的 YAHOO.util.Dom.getElementsBy(function)。我一直在阅读原型 API 文档,但未能找到等效的内容。它需要能够接受任意函数,而不仅仅是选择 CSS 选择器等。谁能向我建议在原型中完成此任务的最佳方法?

I'm moving some code from YUI to javascript and some of it is using YUI's YAHOO.util.Dom.getElementsBy(function). I've been reading through the prototype API docs and haven't been able to find something equivalent. It needs to be able to take an arbitrary function not just select off a CSS selector or the like. Can anyone suggest to me the best way to accomplish this in Prototype?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-09-02 11:30:45

您可以使用美元兑美元函数和过滤函数:

var elts = $("div.big").filter(myFunction);

You can use the dollar-dollar function and the filter function :

var elts = $("div.big").filter(myFunction);
三生池水覆流年 2024-09-02 11:30:45

Jack Sleight 的函数,来自 http://www.codingforums.com/showthread.php? t=83993 基于 getElementsByClassName ,它按原样完成我所需要的,并且可以轻松扩展以采用任意函数:

document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
    var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
    return $A(children).inject([], function(elements, child) {
        var attributeValue = child.getAttribute(attribute);
        if(attributeValue != null) {
            if(!value || attributeValue == value) {
                elements.push(child);
            }
        }
        return elements;
    });
}

A function by Jack Sleight from http://www.codingforums.com/showthread.php?t=83993 based on getElementsByClassName that accomplishes what I need as is and would be easily extended to take an arbitrary function:

document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
    var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
    return $A(children).inject([], function(elements, child) {
        var attributeValue = child.getAttribute(attribute);
        if(attributeValue != null) {
            if(!value || attributeValue == value) {
                elements.push(child);
            }
        }
        return elements;
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文