jQuery:从.text()中排除子项 [英] jQuery: exclude children from .text()

查看:512
本文介绍了jQuery:从.text()中排除子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

jquery从.text()中排除一些子节点

鉴于此HTML:

<a href="#" class="artist">Soulive<span class="create-play">Play</span></a>

我想获得的文本内容(在我的函数上下文中这个没有 span ,所以我留下:

I want to get the text content of the a (which is this in the context of my function) without the text content of the span, so I'm left with:

Soulive

如果我这样做:

$(this).text();

我得到:

SoulivePlay

如何排除 span

推荐答案

微插件:

$.fn.ignore = function(sel){
  return this.clone().find(sel||">*").remove().end();
};

...拥有此HTML:

...having this HTML:

<div id="test"><b>Hello</b><span> World</span>!!!</div>

将导致:

var text = $('#test').ignore("span").text(); // "Hello!!!"
var html = $('#test').ignore("span").html(); // "<b>Hello</b>!!!"






如果你想要它更快和你只需要排除直接的孩子......使用 .children(而不是 .find(


if you want it faster and you need only to exclude the immediate children... use .children( instead of .find(

这篇关于jQuery:从.text()中排除子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆