What is the status of jQuery's multi-argument content syntax: deprecated, supported, documented?
I've never seen this in any jQuery docs I've read; nor, have I ever seen it in the wild. I just observed multi-content syntax working here for the after
modifier with jQuery 1.4.2. Is this supported syntax? Is it deprecated?
$(".section.warranty .warranty_checks :last").after(
$('<div class="little check" />').click( fun开发者_开发问答ction () {
alert('hi')
} )
, $('<span>OEM</span>') /*Notice this (a second) argument */
);
Here is the signature for after: .after( content )
. But, as my example shows it should be .after( content [, content...] )
I've never seen any indication in the jQuery grammar that any of the functions accept more than one argument (content) in such a fashion.
UPDATE: What did it do? I left this out thinking it was obvious:
It inserts a <div class="little check" />
with the aforementioned callback on .click()
and follows it up with a totally new sibling element <span>OEM</span>
.
See this follow-up for a question about how to rewrite this.
Yes this works, though it's not technically supported, you can see a demo here.
If you give .after()
multiple arguments, it'll append them one at a time.
You can see the relevant jQuery core code here: http://github.com/jquery/jquery/blob/master/src/manipulation.js#L141
It takes all the arguments provided and pushes them on the stack for insertion...but since this isn't a documented feature, it may change in any future jQuery release (though it appears safe for at least 1.4.3).
According to the docs it is not allowed, but you see in the code that it is possible (what you however already know from your experiments ;-) )
after: function() {
if ( this[0] && this[0].parentNode ) {
return this.domManip(arguments, false, function( elem ) {
this.parentNode.insertBefore( elem, this.nextSibling );
});
} else if ( arguments.length ) {
var set = this.pushStack( this, "after", arguments );
set.push.apply( set, jQuery(arguments[0]).toArray() );
return set;
}
},
EDIT: I thought that the parameter was being passed to click
.
This answer is correct but irrelevant.
These functions only take one parameter.
Any other parameters will be ignored (for now).
Therefore, the second parameter in your example is ignored.
However, if jQuery ever adds an optional parameter to these functions (eg, a boolean to add a live handler), your example will stop working.
The handlers are defined by jQuery (1.4.2) like this:
jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
"change select submit keydown keypress keyup error").split(" "), function( i, name ) {
// Handle event binding
jQuery.fn[ name ] = function( fn ) {
return fn ? this.bind( name, fn ) : this.trigger( name );
};
if ( jQuery.attrFn ) {
jQuery.attrFn[ name ] = true;
}
});
This code loops through the event names and creates the following function for each event:
jQuery.fn[ name ] = function( fn ) {
return fn ? this.bind( name, fn ) : this.trigger( name );
};
As you can see, the function only uses one parameter.
精彩评论