我有一个动态的 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>
还有一张我的真实结构图:
最佳答案
使用您的#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/