gpt4 book ai didi

jquery - insertAfter 在循环内

转载 作者:行者123 更新时间:2023-12-03 22:38:49 25 4
gpt4 key购买 nike

我有一些由 CMS 生成的代码:

<div class="block">
<a class="link" href="#">Link</a>
<h4>Header here</h4>
<div class="text">Some text here</div>
</div>

我需要将链接移到文本 div 之后。我已经尝试过:

$(document).ready(function() {
$('.block').each(function() {
$('.block a.link').insertAfter('.block div.text');
});
});

但这只会导致链接重复大约 10 次(循环次数。

我尝试使用 $(this) 但我不太明白如何编写正确的语法来在函数中附加 a.link...如下所示:

 $(this).a.link.insertAfter($(this).div.text);

最佳答案

类似这样的东西应该可以工作,使用siblingsafter:

$('.block a.link').each(function() {
$(this).siblings('.text').after(this);
});

这表示“对于每个匹配的元素,找到与 .text 匹配的元素,并在其后面插入原始元素”。

或者,您可以这样做:

$('.block a.link').each(function() {
$(this).parent().append(this);
});

这假定您要将元素放在 div.block 的末尾。

关于jquery - insertAfter 在循环内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6860961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com