jQuery get .text() but not the text in span

若如初见. 提交于 2019-11-30 08:57:54

Yes, you can select only the text contents of the element, like this:

 var text = '';
 $('a').contents().each(function(){
    if(this.nodeType === 3){
     text += this.wholeText;
    }
 });
 $("#largemenutop").html(text);
Gary Green

Nice solution Herman, though it can be reduced to something like this:

JS

$('li a').contents().filter(function() {
    return this.nodeType == 3;
}).text();

HTML

<li><a href="#">Apple<span>hi</span> Juice</a></li>

Will return Apple Juice

Fiddle: http://jsfiddle.net/49sHa/1/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!