jQuery 换行语法

发布于 2024-08-20 01:02:35 字数 748 浏览 6 评论 0原文

一天结束了。我希望我只是逻辑错误。

我无法让这个工作:

var $divA= $("<div></div>").addClass('classA');

var $divB= $("<div></div>").addClass('classB');

$myDiv.after($divA.wrap($divB));

上面应该把这个:

<div id="myDiv"></div>

变成这个:

<div id="myDiv"></div>
<div class="classB">
    <div class="classA"></div>
</div>

但它似乎不适用于那里的“包装”。我没有收到任何错误,它只是不将 divA 与 divB 包装在一起,而只是自行插入 divA 。

我是不是对包装有什么误解?

更新:

一个更简单的示例,但不起作用:

$myBox.after($("<p></p>").wrap("<div></div>"));

这将仅在 myBox 之后添加 DIV。

看起来 jQuery 不喜欢在之后添加换行。

It's end of day. I'm hoping I'm just having a lapse of logic.

I can't get this to work:

var $divA= $("<div></div>").addClass('classA');

var $divB= $("<div></div>").addClass('classB');

$myDiv.after($divA.wrap($divB));

The above should turn this:

<div id="myDiv"></div>

Into this:

<div id="myDiv"></div>
<div class="classB">
    <div class="classA"></div>
</div>

But it doesn't seem to work with that 'wrap' in there. I don't get any errors, it just doesn't wrap divA with divB and just inserts divA by itself.

Am I misunderstanding wrap?

UPDATE:

A simpler example that does not work:

$myBox.after($("<p></p>").wrap("<div></div>"));

That will add just the DIV after myBox.

It seems like jQuery doesn't like wrap added to after.

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

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

发布评论

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

评论(6

雪落纷纷 2024-08-27 01:02:35

您是否

$myDiv.after($divA.wrap('<div class="divB"></div>'));

只是为了测试目的而尝试过?

据我了解,您不应该将 jQuery 对象传递给包装函数:

.wrap() 函数可以采用任何
可以传递的字符串或对象
到 $() 工厂函数来指定
DOM 结构。这个结构可能是
嵌套了几层深,但应该
仅包含一个最内部元素。这
结构将包裹在每个
匹配集合中的元素
元素。


如果上面的例子有效,那么这就是原因;-)

Have you tried

$myDiv.after($divA.wrap('<div class="divB"></div>'));

just for testing purposes?

As far as I understand, you shouldn't pass a jQuery object to the wrap-function:

The .wrap() function can take any
string or object that could be passed
to the $() factory function to specify
a DOM structure
. This structure may be
nested several levels deep, but should
contain only one inmost element. The
structure will be wrapped around each
of the elements in the set of matched
elements.

If the example above works, then this is the reason ;-)

贪了杯 2024-08-27 01:02:35

只是偶然发现这个线程有同样的问题: jQuery .wrap() 不工作

我想想如果你将代码从

$myDiv.after($divA.wrap($divB)) 更改为 $myDiv.after($divA.wrap($divB).parent())

你会得到你的包裹。

Just stumbled onto this thread having the same problem: jQuery .wrap() isn't working

I think if you change your code from

$myDiv.after($divA.wrap($divB)) to $myDiv.after($divA.wrap($divB).parent())

you'll get your wrap.

っ左 2024-08-27 01:02:35

让您感到困惑的部分可能是 .wrap() 返回内部元素,而不是父元素。

因此,您必须像这样使用包装对象的 parent 对象:

var $divA= $("<div/>").addClass('classA'),
    $divB= $("<div/>").addClass('classB');

$myDiv.after($divA.wrap($divB).parent());

($divA.parent() 等于 $divB 之后因此

关键部分是 $divA.wrap($divB) 返回 $divA,而不是 $divB

请参阅参考:

此方法返回用于链接的原始元素集
目的。

http://api.jquery.com/wrap/

The confusing part might have been to You that .wrap() returns the inner element, not the parent element.

So you have to use the parent object of the wrapped one like this:

var $divA= $("<div/>").addClass('classA'),
    $divB= $("<div/>").addClass('classB');

$myDiv.after($divA.wrap($divB).parent());

($divA.parent() is equal to $divB after the wrapping)

So the key part is that $divA.wrap($divB) returns $divA, NOT $divB

see the reference:

This method returns the original set of elements for chaining
purposes.

http://api.jquery.com/wrap/

归途 2024-08-27 01:02:35

我得到了这个工作:

$('div#myDiv').after("<div class='classA'></div>").end().find('.classA').wrap("<div class='classB'></div>");

用你的 html 来解决你最初的问题。 这里是 jQuery 的 end 函数的源代码。此代码将使链上升一级(到 myDiv 级别),然后根据该级别的 find('.classA') 进行换行。这会发现你的div添加了after,并用classB将其包装在div中。

编辑:
好的,我认为这会按照您想要的方式为您工作:

var divA= $("<div></div>").addClass('classA');
$('div#myDiv').after($(divA).wrap('<div class="divB" />'));

我认为问题是当在 divA 上调用 wrap 时,它需要是一个 jQuery 对象才能正常工作。所以实际上您所缺少的就是将 divA 包装在 () 中。

I got this to work:

$('div#myDiv').after("<div class='classA'></div>").end().find('.classA').wrap("<div class='classB'></div>");

with your html to solve your initial question. Here's the source for jQuery's end function. This code will go make the chain go up one level (to the myDiv level), and will then wrap based on the find('.classA') at that level. That will find your div added with after, and wrap it in div with classB.

Edit:
Ok, I think this will work for you the way you want:

var divA= $("<div></div>").addClass('classA');
$('div#myDiv').after($(divA).wrap('<div class="divB" />'));

I think the problem was that when calling wrap on divA, it needs to be a jQuery object to work correctly. So really all you were missing was wrapping divA in ().

节枝 2024-08-27 01:02:35

你不是在寻找包装。你想要:

divB.append(divA).insertAfter('#myDiv');

You're not looking for wrap. You want:

divB.append(divA).insertAfter('#myDiv');
梅窗月明清似水 2024-08-27 01:02:35

我可能读错了,但是如果你使用 jQuery,$ 不是保留字符吗?您是否尝试过仅设置变量名称以便他们不使用它?

如果是这样:

var divA = $("<div></div>").addClass('classA');
divA.wrap("<div class=\"classB\"></div>");
divA.insertAfter($("#myDiv"));

或者

divA.after($("#myDiv"));

这是关于此事的 jQuery 文档

I may be reading this wrong, but if you're using jQuery, isn't the $ a reserved character? have you tried just setting your variable names so they don't use it?

If so:

var divA = $("<div></div>").addClass('classA');
divA.wrap("<div class=\"classB\"></div>");
divA.insertAfter($("#myDiv"));

or

divA.after($("#myDiv"));

Here's the jQuery docs on the matter.

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