gpt4 book ai didi

jquery insertbefore 多个元素

转载 作者:行者123 更新时间:2023-12-01 04:53:24 24 4
gpt4 key购买 nike

我还创建了一个jsfiddle来演示我的问题。

我需要在标题之前有时间。

<ul> 
<li>
<h3>Title 1</h3>
<p>Speaker 1</p>
<p class="ui-li-aside"><b>Time 1</b></p>
</li>
<li>
<h3>Title 2</h3>
<p>Speaker 2</p>
<p class="ui-li-aside"><b>Time 2</b></p>
</li>
<li>
<h3>Title 3</h3>
<p>Speaker 3</p>
<p class="ui-li-aside"><b>Time 3</b></p>
</li>

这有效:

$("li:nth-child(0) p.ui-li-aside").insertBefore("li:nth-child(0) h3");

但我不想对每个元素都这样做。

这并没有按照我想要的方式工作:

$("li:nth-child(n) p.ui-li-aside").insertBefore("li:nth-child(n) h3");

如何以更短的方式针对任何元素编写此内容?我无法更改 html 部分,因为它也在其他页面上使用。

最佳答案

您可以使用prependTo

$(document).ready(function () {
$('li').each(function () {
var $this = $(this);
$('p.ui-li-aside', $this).prependTo($this);
});
});

<强> Check Fiddle

关于jquery insertbefore 多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16957664/

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