jQuery使用:not从div中选择文本 [英] jQuery selecting text from a div with :not

查看:156
本文介绍了jQuery使用:not从div中选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小提琴: http://jsfiddle.net/hAzJq/

如何在div内而不是跨度内选择文本?

How can i select the text inside the div but not inside the span?

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

推荐答案

首先使用.contents,这将提取文本节点和HTML节点,并应用:not选择器以过滤出span:

Use .contents first, which will extract both text nodes and HTML nodes, and apply a :not selector to filter out the span:

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

也就是说,一种更简单的方法可能是将一个带有特殊类的span应用于您想要的内容,然后选择它.

That said, an easier way might be to apply a different span with a special class around just the content you do want, and select that.

这篇关于jQuery使用:not从div中选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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