Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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 - Fatal编程技术网

jQuery中的纯文本选择器

jQuery中的纯文本选择器,jquery,Jquery,我想保留第一个和,但删除以下内容中的其余部分: <div> <a href="http://aerospace.com">Aerospace</a> and <a href="http://aviation.com">Aviation</a> <span class="blah">(15)</span> </div> 但是和文本留在那里 可以匹配jQuery中的文本吗?是否

我想保留第一个
,但删除以下内容中的其余部分:

<div>
    <a href="http://aerospace.com">Aerospace</a> and 
    <a href="http://aviation.com">Aviation</a>
    <span class="blah">(15)</span>
</div>
但是
文本留在那里

可以匹配jQuery中的文本吗?是否有一个文本选择器,这样我就可以使用类似于
过滤器(“:isText()”)

在我的脑海中,类似这样的事情应该会起作用,您可以向上下降到父级并对所有内容进行操作,而不仅仅是
元素
节点具有
节点类型
1
,还包括具有
节点类型
节点


如果不行,请告诉我。

复制
span
,然后从
div
中删除所有内容,然后将
span
放回怎么样

$("a:contains('Aerospace')").each(function()
{
    var span = $(this).parent().children("span").clone();
    $(this).parent().empty().append(span);
});

innerText
应该是
nodeValue
?我正在针对IE8/FF3.5进行测试,innerText始终未定义,但nodeValue似乎可以工作。另外,您如何删除文本而不是显示警报框?您是否在半小时前试用了我的最新版本?innerText用于IE,textContent用于现代DOM解析器。它应该使用modern并回退到innerText。您可以尝试将nodeValue/innerText/textContent重置为空以删除它。为什么不使用
var text=$(this.text()
?jQuery使事物与浏览器兼容。
$("a:contains('Aerospace')").parent().contents().each(function() {
    if ( this.nodeType == 3 ) {
        var text = this.textContent? this.textContent: this.innerText;
        text = $.trim( text );
        if ( text.length ) {
            alert( text );
        }
    }
});
$("a:contains('Aerospace')").each(function()
{
    var span = $(this).parent().children("span").clone();
    $(this).parent().empty().append(span);
});