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

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

问题描述

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

<span><strong>bla bla bla</strong>我想要这个文本</span>

我只想要没有强标签的文本我想要这个文本".我该怎么做?

解决方案

试试这个...

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

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?

解决方案

Try this...

<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天全站免登陆