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);
});