如何将函数附加到JQuery find()函数?

时间:2015-04-20 12:47:17

标签: javascript jquery find anonymous-function

我有一个我希望在其中搜索类的元素数组。一旦找到,我就想附加一个函数,这样我就可以操作子元素了。我的研究让我进入了每个循环和查找功能。我想做这样的事情:

$(arrayOfElems).each(function( i ) {
    $(this).find('.something').function() {
        console.log('found element: '+$(this));
    }
}

请原谅糟糕的代码!

3 个答案:

答案 0 :(得分:2)

使用.each函数https://api.jquery.com/each/

$(this).find('.something').each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

答案 1 :(得分:0)

$(arrayOfElems).each(function( i ) {
    ManipulateItem($(this).find('.something'));
 }

 function ManipulateItem(item){
       //do stuff
 }

答案 2 :(得分:0)

根据代码的结构,您可能需要额外的循环。

   $("ul.StuffToloop").each(function (i, item) {
                if (item.classList == "ClassToDoThings") {//first check your array of elements
                   $( item).append( "<p>Test</p>" );//if matched add your function that does things to child elements
                    }
                }
相关问题