可以在文档之外的元素上使用 jQuery 吗?

发布于 2024-12-16 01:09:03 字数 700 浏览 0 评论 0原文

使用一些遗留架构,并且由于初始化序列的性质,我需要在将元素添加到文档之前对其进行包装。假设我有以下内容:

<div id="containerX">
   <div id="myNode"></div>
</div>

并且我需要在将“myNode”添加到 DOM 之前对其进行包装。 jQuery 选择器在这种情况下还能工作吗?如果是这样,我怎样才能做到这一点?我尝试像这样传递元素:(

更正了下面一些答案中提到的一些拼写错误):

$(this.element).wrap('<div id="'+ "myWrapper_" + this.id + '"></div>');

没有运气。我假设选择器的常用语法不起作用,因为节点位于文档之外。我发现的最接近的是这篇文章: Manipulate DOM元素,然后将它们添加到文档,但我的情况和他的情况之间的区别是我没有字符串,我有使用 document.createElement 创建的尚未附加的元素。

谁能指出我正确的方向,或者这是否可能?

谢谢

Working with some legacy architecture and because of the nature of the initialization sequence I need to wrap an element before it's been added to the document. Say I have the following:

<div id="containerX">
   <div id="myNode"></div>
</div>

And I need to wrap "myNode" before it's added to the DOM. Do jQuery selectors even work in this context? If so, how can I make that happen? I've tried passing in the element like so:

(Corrected some typos here referred to in some answers below):

$(this.element).wrap('<div id="'+ "myWrapper_" + this.id + '"></div>');

with no luck. I'm assuming that the usual syntax for selectors won't work since the nodes are outside the document. The closest thing I've found was this post here: Manipulate DOM elements before adding them to the document but the difference between my situation and his is I don't have strings, I have elements created with document.createElement that have not been appended.

Can anyone point me in the right direction or is this even possible?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紧拥背影 2024-12-23 01:09:03

您也可以在其他元素上使用 Jquery,我不止一次使用它来解析 AJAX 请求的响应。

虽然我不确定您的确切请求是否可行,但我认为代码中的主要问题是您尝试环绕元素的 div 之前缺少 <

You can use Jquery on other elements as as well, and I've more than once used it to parse a response from an AJAX request.

Though I am not sure if your exact request is possible, I think the main problem in your code is the missing < before the div you're trying to wrap around your element.

兔姬 2024-12-23 01:09:03

jQuery 允许您处理尚未添加到文档中的元素。它们可以是简单的 HTML 字符串,也可以是使用 createElement() 构造的元素。

让我给你一个简单的例子:

var container=document.createElement('div');
container.id='containerX';
var mynode=document.createElement('div');
mynode.id='myNode';
container.appendChild(mynode);

$(container).find('#myNode').wrap('<div id="myWrapper'+mynode.id+'">');

这应该会产生你想要的结构。您只需应用变量名称即可。我这样做是因为我不确定你的 this.element 包含什么。

请查看 jsFiddle 演示

jQuery lets you work on elements not added to the document yet. They can be simple HTML strings as well as elements constructed with createElement().

Let me give you a quick example:

var container=document.createElement('div');
container.id='containerX';
var mynode=document.createElement('div');
mynode.id='myNode';
container.appendChild(mynode);

$(container).find('#myNode').wrap('<div id="myWrapper'+mynode.id+'">');

This should result in the structure you wanted. You just have to apply your variable names. I did it this way because I am unsure what your this.element contains.

Please check out the jsFiddle Demo.

情痴 2024-12-23 01:09:03

首先,您的代码中有错误。你少了一个“+”。试试这个:

$(this.element).wrap('div id="' + myWrapper + this.id + '"></div>');

注意“myWrapper”之前的“+”。

不过我认为你是对的,你不能在 DOM 之外的元素上使用换行。您始终可以将其添加到 DOM,然后包装它。如果您不希望它出现在 DOM 中,则可以调用remove 将其取回;当然,这意味着删除其父元素,而不是原始元素本身。我自己还没有尝试过所有这些,但它应该有效。您需要父级(包装器)有一个唯一的 id,以便您可以确保抓住它。另一件事,看起来您希望父级与您正在包装的元素具有相同的 id。您应该避免将相同的 id 分配给两个元素。因此,如果父级获得了 id,那么应该将其从元素中删除。

First, you have an error in your code. You're missing a "+". Try this:

$(this.element).wrap('div id="' + myWrapper + this.id + '"></div>');

Note the "+" before "myWrapper".

However I think you're right, you can't use wrap on an element outside the DOM. You can always add it to the DOM, then wrap it. And if you don't want it in the DOM, then call remove to get it back; of course, this would be mean removing its parent, rather than the original element itself. Haven't tried all of this myself but it should work. You would need the parent (wrapper) to have a unique id so that you can be sure to grab it. One other thing, it looks like you want the parent to have the same id as the element you're wrapping. You should avoid assigning the same id to two elements. So if the parent gets the id, then it should be removed from element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文