jQuery从.text()中排除一些子节点 [英] jquery exclude some child nodes from .text()

查看:150
本文介绍了jQuery从.text()中排除一些子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

html结构看起来像这样

The html structure looks like this

<div id="parent">
    parent may contain text
    <div id="child1">
       child 1 may contain text
       <script>console.log('i always contain something');</script>`
    </div>

    <div id="child2">
       child2 may contian text
    </div>    
</div> 

我正在尝试获取除<script>内容以外的每个节点的内容.结果应如下所示:

I am trying to get contents of every node except the contents of <script>. The result should look like this:

    parent may contain text
    child 1 may contain text 
    child2 may contian text

我尝试使用($('#parent').not('div script').text(),但是它不起作用

I've tried using ($('#parent').not('div script').text() ,but it does not work

推荐答案

尝试一下:

($('#parent').text()).replace($('#parent script').text(),'');

查看此小提琴.

这篇关于jQuery从.text()中排除一些子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆