gpt4 book ai didi

javascript - 如何选择特定元素顶部的所有子元素?

转载 作者:行者123 更新时间:2023-11-28 01:20:33 25 4
gpt4 key购买 nike

我有一个动态的 HTML 结构。所以,我想选择 div.test 后面的所有内容:

<div>
/* I want to select everythis is here */
<span class = "test">
</div>

示例 1:

<div>
hello
<span class = "test">
</div>

我想要这个输出:hello


示例 2:

<div>
this is a <a href="www.example.com">test</a>
<span class = "test">
</div>

我想要这个输出:this is a <a href="www.example.com">test</a>

还有一张我的真实结构图:

enter image description here

最佳答案

使用您的#2 示例:

<div>
this is a <a href="www.example.com">test</a>
<span class = "test">
</div>

使用这个jquery:

var content = $('div').html().split($('span.test')[0].outerHTML)[0].trim();

console.log(content);

它会输出:

this is a <a href="www.example.com">test</a>

http://jsfiddle.net/swdn2ews/1/


编辑:

这是与 OP 的真实结构最接近的 jsfiddle:http://jsfiddle.net/bbvyps2p/1/

关于javascript - 如何选择特定元素顶部的所有子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34112925/

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