jquery - 获取没有子文本的元素的文本 [英] jquery - get text for element without children text

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

问题描述

例如我们有这个文件

<div id="mydiv">
    some text here 
    <div id="inner div">
         text for inner div
    </div>
</div>

我需要 #mydiv 仅限文字使用这样的代码:

i need to get #mydiv text only with some code like this :

alert($('#mydiv').text());  // will alert "some text here"


推荐答案

嘿试试请: http://jsfiddle.net/MtVxx/2/

此处您的具体案例的良好链接: http://viralpatel.net/blogs/jquery-get-text-element-without-child-element/ (这只会获取元素的文本)

Good link for your specific case in here: http://viralpatel.net/blogs/jquery-get-text-element-without-child-element/ (This will only get the text of the element)

希望这有帮助,:)

代码

jQuery.fn.justtext = function() {

    return $(this).clone()
            .children()
            .remove()
            .end()
            .text();

};

alert($('#mydiv').justtext());​

这篇关于jquery - 获取没有子文本的元素的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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