有没有一种技巧可以将任意内容注入(inject)到 DOM 中,然后在不知道内容是什么的情况下选择该内容?
示例:
function myInjector( _htmlElement ) {
$('#target').replaceWith(_htmlElement);
/* Is it possible to then select the injected element here? */
}
感谢您提供的任何建议。
谢谢。
最佳答案
您不需要选择它 - 您已经在 _htmlElement
变量中存储了对它的引用:
function myInjector( _htmlElement ) {
$('#target').replaceWith(_htmlElement);
alert(_htmlElement.parentNode.tagName);
}
<小时/>
编辑 — 只需首先创建新的 jQuery 对象并将其存储在变量中:
function myInjector( _htmlElement ) {
var newEl = $(_htmlElement);
$('#target').replaceWith(newEl);
alert(newEl.parent()[0].tagName);
}
关于javascript - jQuery:在 DOM 注入(inject)后选择元素(当元素未知时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4967844/