Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何仅获取元素中的文本值_Jquery_Html - Fatal编程技术网

Jquery 如何仅获取元素中的文本值

Jquery 如何仅获取元素中的文本值,jquery,html,Jquery,Html,我的html是: <div> Some text <span>label text</span> </div> 我希望输出打印为- "Some text" 当我执行$(“div”).text()时,我会得到一些文本标签文本 当我做$(“div”).html()时,我会得到一些文本标签文本试试这个plz: 工作演示您还可以查找innerHTMl和outterHTML jQuery.fn.justtext = function() {

我的html是:

<div>
   Some text
   <span>label text</span>
</div>
我希望输出打印为-

"Some text"
当我执行
$(“div”).text()
时,我会得到一些文本标签文本

当我做
$(“div”).html()
时,我会得到
一些文本标签文本

试试这个plz:

工作演示您还可以查找
innerHTMl
outterHTML

jQuery.fn.justtext = function() {

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

};
alert($('div').justtext());​
试试这个plz:

工作演示您还可以查找
innerHTMl
outterHTML

jQuery.fn.justtext = function() {

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

};
alert($('div').justtext());​
给你

预览-

给你

预览-

这可能有效:

var txt = $('div').contents().filter(function() {
    return this.nodeType == 3;
});

alert(txt.text());
这可能会起作用:

var txt = $('div').contents().filter(function() {
    return this.nodeType == 3;
});

alert(txt.text());

@亚历克斯-我不明白。not('span')将检查元素是否不是span。“这有什么用呢?”亚历克斯——我不明白。not('span')将检查元素是否不是span。那有什么用呢。