Jquery version of yui .getElementsBy

Jquery version of yui .getElementsBy

本文关键字:getElementsBy yui of version Jquery      更新时间:2023-09-26

我正在尝试为web应用程序从YUI更新到jQuery;不过,我对在jQuery工作还是个新手。我在YUI:中有这个代码块

function getChildUL(childEl){
    var childrenUL = YAHOO.util.Dom.getElementsBy(
        function(element) { 
            return element.parentNode == childEl; 
        }, "UL", childEl);
    childrenUL = childrenUL[childrenUL.length-1];
    return childrenUL;
}

我找到了这个链接,并尝试根据接受的答案在jQuery中重写该块。我越来越不适合儿童UL。这是我的尝试:

function getChildUL(childEl){
    var childrenUL = $("UL").filter(function(element) { 
        return element.parentNode == childEl; 
    }).get();
    childrenUL = childrenUL[childrenUL.length-1];
    return childrenUL;
}

感谢您的帮助。

您的代码中有一个错误:parentNode不是jQuery函数。我认为您应该使用parent()closest()数。