使用JQuery提取元素中的文本 [英] Extract the Text in a Element with JQuery

查看:99
本文介绍了使用JQuery提取元素中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JQuery提取元素内的文本

I want to extract the Text inside a Element with JQuery

<div id="bla">
    <span><strong>bla bla bla</strong>I want this text</span>
</div>

我只需要文本我想要此文本",而没有强标签.我该怎么办?

I want only the text "I want this text" without the strong-tag. How can I do that?

推荐答案

尝试一下...

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
    $("#bla span").contents().each(function(i) {
        if(this.nodeName == "#text") alert(this.textContent);
    });
});

//]]>
</script>

这不需要从上下文中删除任何其他节点,只需在每次迭代中为您提供文本节点即可.

This doesn't need to remove any other nodes from context, and will just give you the text node(s) on each iteration.

这篇关于使用JQuery提取元素中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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