gpt4 book ai didi

jquery - 使用jquery合并多个相同的html元素

转载 作者:行者123 更新时间:2023-12-01 08:09:01 25 4
gpt4 key购买 nike

我喜欢只在 div 中移动文本,使其类似于:

 <div class="field-item even">
Job1<p><a href="">Name1</a></p>
</div>

等等...

我喜欢合并并删除所有 .field-name-field-brief-text-team div

请帮忙...谢谢

 <div class="field field-name-field-team field-type-entityreference">
<label>Team</label>
<div class="field-items">

<div class="field-item even">
<p><a href="">Name1</a></p>
</div>

<div class="field-item odd">
<p><a href="">Name2</a></p>
</div>

<div class="field-item even">
<p><a href="">Name3</a></p>
</div>

</div>
</div>

<div class="field field-name-field-brief-text-team field-type-text">
<div class="field-items">

<div class="field-item even">Job1</div>
<div class="field-item odd">Job2</div>
<div class="field-item even">Job3</div>

</div>
</div>

最佳答案

我相信这可以满足您的要求:

$('.field-name-field-brief-text-team .field-item').each(function (i) {
var to = $('.field-item').eq(i)[0],
toFirst = to.getElementsByTagName('*')[0],
from = this,
txt = document.createTextNode(this.textContent);
to.insertBefore(txt,toFirst);
}).remove();

JS Fiddle demo .

上面的版本略有改变,使用另一种方式从 jQuery 选择器检索 DOM 节点(使用 get(0) )并查找 to 的第一个子元素。节点( to.firstChild )。这些只是替代方案,并不一定比以前的方法更推荐:

$('.field-name-field-brief-text-team .field-item').each(function (i) {
var to = $('.field-item').eq(i).get(0),
toFirst = to.firstChild,
from = this,
txt = document.createTextNode(this.textContent);
to.insertBefore(txt,toFirst);
}).remove();

JS Fiddle demo .

编辑以提供最快的(根据 JS Perf )方法:

$('.field-name-field-brief-text-team .field-item').each(function (i) {
var to = $('.field-item')[i]
toFirst = to.firstChild,
txt = document.createTextNode(this.textContent);
to.insertBefore(txt,toFirst);
}).remove();

JS Fiddle demo .

<小时/>

编辑以添加请求的内容(在下面的评论中):到正文:

$('.field-name-field-brief-text-team .field-item').each(function (i) {
var to = $('.field-item')[i]
toFirst = to.firstChild,
txt = document.createTextNode(this.textContent + ': ');
to.insertBefore(txt, toFirst);
}).remove();

JS Fiddle demo .

引用文献:

关于jquery - 使用jquery合并多个相同的html元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14813717/

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