I have the following jQuery code used to insert a new DOM element. someHTML
in this case is just a HTML string.. like <div>text</div>
.
$area.prepend(someHTML);
I'd like to get a reference to the node after it is in开发者_运维知识库serted in the DOMTree, with properly set parentNode and what-not.
jQuery filter functions like .find() and .filter() won't work for my situation since both create new jQuery objects. I need the actual reference to the DOMNode.
Any ideas?
How about doing this the other way round, with prependTo
? Construct the DOM node with the jQuery constructor, then prepend it to your existing selection, then use get
to get the underlying DOM node.
var node = $(someHTML).prependTo($area).get(0);
Why don't you create a variable for it?
var $someHTML = $(someHTML);
$area.prepend($someHTML);
//now you can reference $someHTML elsewhere
The other answers work fine, but another option is to give the new div
an id
when you create it. Then it can be referenced the normal way anywhere after it's been added to the DOM:
$('#someElement').prepend('<div id="something"></div>"');
$('#something').html('whatever');
精彩评论