Reputation: 71
From jquery site :
Methods called on jQuery selections are in the $.fn namespace, and automatically receive and return the selection as 'this'.
but if i use $('selector').clone() ,won't the returned object be the cloned one rather than the original? In general,how to know what will be the return object of a jquery method?
Upvotes: 2
Views: 86
Reputation: 1
If interpret Question at OP accurately, requirement to determine which object
returned ? Whether original
or clone
?
For element
, or selector
clones , clone
object do not appear to possess selector
property at object
that returns > 0
, i.e.g., try
$(function () {
var original = $("div");
var clone = $("div").clone(true, true);
console.log(original.selector.length > 0, clone.selector.length > 0);
// `true` , `false`
$.fn.isClone = function () {
return !$(this).selector.length > 0
};
console.log($(clone).isClone(), $(original).isClone());
// `true` , `false`
});
jsfiddle http://jsfiddle.net/guest271314/LZaTM/
Upvotes: 0
Reputation: 17579
Idea here is that you can chain jquery methods. Some of the methods might change selection and some not, but they have to return the set, so they can be chained. For instance
$('.pages').filter('.inactive').hide().end().filter('.active').show()
In this case method hide
will receive selection as this
and will do what it is supposed to do and return this
unchanged.
But filter
method on the opposite will receive full selection but will change it and returned changed selection as this
.
Really cool thing to notice here is that you can actually use end
to get previous selection to do your stuff.
Upvotes: 1
Reputation: 69260
For clone()
, the documentation you quote looks wrong.
The end of the clone()
implementation clearly states that it is the cloned set that is returned.
jQuery.extend({
clone: function( elem, dataAndEvents, deepDataAndEvents ) {
var i, l, srcElements, destElements,
clone = elem.cloneNode( true ),
// --- Stuff removed for brevity ---
// Return the cloned set
return clone;
}
Upvotes: 1