How i can do this in jQuery? any trick to pass selector instead of jQuery object in $
In JavaScript if I append a child which has an ID to another place then it's removed from original location where they currentl开发者_开发知识库y are.
In javascript I have an event where I can get selector by using this inside the function
$('.').event(function(){
this
});
This is passed to another function and they work fine. Now I want to pass the clone instead of the object; and remember that this does not have ID.
The old code works by passing this
to function as DoSomething(this)
if I make a clone using jQuery clone
then I have the jQuery object. So how do I get a reference to this
instead of the jQuery object when working with the clone?
var clone = $(this).clone() // this is jQuery object.
//how do I get this out of clone?
if I append a child which has an ID to another place then it's removed from original location where they currently are.
Yes, but the same is true of a child node that doesn't have an id
attribute as well. An id
is only an easy way for you to get a reference to the Element node object; it makes no difference to DOM insertion of cloning behaviour.
In javascript I have an event where I can get selector by using
this
inside the function
No, this
in an event handler gives you the DOM Element node object, not a selector string. A Node can be turned into a jQuery wrapper around it using $(node)
and a selector can be turned into a jQuery wrapper on the list of matching nodes using $(selector)
but other than this overloading in the jQuery API they're completely different animals.
To pull a Node back out of a jQuery wrapper you can use the get()
method or simple array-like access:
var clonedNode= $(this).clone()[0];
var clonedNode= $(this).clone().get(0);
to taste. (get()
has some extra features which you don't need here.)
To get the selector used to create a jQuery wrapper you can use the selector
property, but this won't return anything if the wrapper was created from a node object ($(this)
) rather than a selector.
$(this).clone().get(0)
. This will get the first matching DOMElement from the jQUery object.
To get the DOMElement object from a jQuery object use get(0):
var clone = $(this).clone(); // this is jQuery object.
var el = clone.get(0); // this is DOMElement object
精彩评论