开发者

Getting an element as jQuery object from a set of elements

Is there a way to get an element from a jQuery object, but as jQuery object?

If I wanted to get the text of elements, presently I'd write it like this:

var elements = $(".set");
for (var idx=0; idx<elements.size(); idx++) {
    var text = $(elements.get(idx)).text();
    // do something with text
}

Note that I have to wrap elements.get(idx) in anot开发者_JS百科her jQuery call $().

I realize this isn't that big a deal, I just want to make sure I'm not doing extra work if there is an easier way.


Have a look at .eq() [docs]:

// returns the third div (zero-indexed) in a jQuery wrapper
var $mydiv = $('div').eq(2); 

Is this what you mean?


Why not go with:

$(".set").each(function (idx) {
   var text = $(this).text();
    // do something with text
});


you can use:

$(".set").each( function () {
   var text = $(this).text();
   // do stuff
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜