Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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使用:not从div中选择文本_Jquery - Fatal编程技术网

jQuery使用:not从div中选择文本

jQuery使用:not从div中选择文本,jquery,Jquery,我做了一把小提琴: 如何选择div内的文本而不是span内的文本 alert($("div").not('span').text());​ 使用contents()方法从div获取所有内容,然后过滤掉不需要的内容 $("div").contents().filter(function(){ return !$(this).is('span');}).text(); 您还可以将选择器传递给contents()方法 alert($("div").contents(':not(span)').te

我做了一把小提琴:

如何选择div内的文本而不是span内的文本

alert($("div").not('span').text());​

使用
contents()
方法从
div
获取所有内容,然后过滤掉不需要的内容

$("div").contents().filter(function(){ return !$(this).is('span');}).text();
您还可以将选择器传递给
contents()
方法

alert($("div").contents(':not(span)').text());​​​​​​​​​​​​
工作演示-

参考资料:

  • contents()
    -
  • filter()
    -

首先使用
.contents
,它将同时提取文本节点和HTML节点,并应用
:not
选择器来过滤
span

alert($("div").contents(':not(span)').text())​​​​​​​​​​​​;
也就是说,一个更简单的方法可能是应用一个不同的
span
,在您想要的内容周围使用一个特殊的类,然后选择它