jQuery multiple selectors order with "this" -


the order of 'i' , 'this' matters. first one, icon added behind button. second one, icon added before button. why that?

example 1:

$('.btn').click(function(){     $('i', this).removeclass().addclass("icon-down"); }) 

example 2:

$('.btn').click(function(){     $(this, 'i').removeclass().addclass("icon-down"); }) 

it clear these 2 forms using generate different results.

$('i', this) 

this form find <i> elements children of element pointed this. structurally equivalent $(this).find('i') preferred way write because think it's more readable.

from jquery doc, form documented jquery( selector [, context ] ).


your other form:

$(this, 'i') 

is not directly supported option jquery function there not documented options take (element, string). check jquery code, going interpreted $(element) form , $(this) second argument getting ignored. option documented jquery( element ). second argument ignored if first argument dom element.

so, first option finds child <i> elements. second option operates on button itself.

edit: in looking @ jquery source code, i've verified $(this, 'i') treated same $(this). in fact, operative part of jquery function:

init: function( selector, context, rootjquery ) {     if ( typeof selector === "string" ) {         // handle selector string here             // ...      // handle: $(domelement)     } else if ( selector.nodetype ) {         this.context = this[0] = selector;         this.length = 1;         return this;     } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -