1 javascript css jquery jquery-selectors
我有这个代码:
<div class="bodytext1typeenc">Ce qu’il faut retenir
<div class="al">Budget «solidarités».</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想只获得"Ce qu'il faut retenir".我试过这个:
$('.bodytext1typeenc').text() // --> doesn't work.
$('.bodytext1typeenc').remove('.al') // --> doesn't work.
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?谢谢 !
您可以克隆,删除子项并获取文本.见下文,
var $clone = $('.bodytext1typeenc').clone();
$clone.children().remove()
$clone.text(); //should return the div's text
Run Code Online (Sandbox Code Playgroud)
注意:如果您不想保留原始内容,则无需克隆.
演示: http ://jsfiddle.net/PX2yA/