开发者

Appending to $(this)

Within a function loop, I need to traverse further into the DOM.

$('.panel_wrapper .panel').each(function(index) {
   // how can I append to this.  For ins开发者_如何学Pythontance another class = "foo"
   $(this) .foo  /* does not work */

This is similar to what I'm trying to do in a function

$(this) = $('.panel_wrapper .panel');
$(this) .foo

The following is a better example:

$('.panel-wrapper .panel').each(function(index) {
  $('.panel-wrapper .panel:eq(' + index + ') .collection_alt_thumbs img:eq(0)').css({"border-color" : "#9B9B9B", "opacity" : 1});           
}); 

Within the function there are additional operations that will be performed, not necessary after the first operation but after a conditional statement. Can the following be performed:

$('.panel-wrapper .panel').each(function(index) {
   $(this)  ('.collection_alt_thumbs img:eq(0)').css({"border-color" : "#9B9B9B", "opacity" : 1});  
   // conditional statement 
   $(this) /* some other element */

}); 


You could use the .find() function:

$(this).find('.foo');


To append a class you should use

$(this).addclass("foo")

it really depends on what you're trying to do if you want direct access to the DOM element itself you shouldn't wrap it at all

this.foo =...

will add foo to the DOM element itself (inside the .each function)


Do you want to append to the selectors (i.e. further limit the items selected) or append to the items selected? @Darin's solution will do the former; if you want to select more items and traverse over them as well you can do one of the following:

$('.panel_wrapper .panel').add('.foo').each(function(index) {

or

$('.panel_wrapper .panel, .foo').each(function(index) {
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜