使用jQuery查找文本元素 [英] finding elements with text using jQuery

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

问题描述

我想创建一个div中包含文本字符串的所有html元素组成的数组,例如

I want to create an array of all the html elements within a div that contain text strings, such as

<p>some string</p>.  

我不想掌握字符串,我希望数组项成为元素(在示例中,将成为p节点).我事先不知道字符串将是什么,所以我找不到匹配的字符串值.我也不希望空文本节点最终出现在数组中.

I don't want to get hold of the strings, I want the array items to be the elements (in the example, would be the p node). I do not know before hand what the strings will be, so I can't look for string values to match. I also don't want empty text nodes to end up in the array.

谢谢!

推荐答案

$("#my_div *").filter(function()
{
     var $this = $(this);
     return $this.children().length == 0 && $.trim($this.text()).length > 0;
})

此版本将不返回包含具有文本元素的父元素,仅返回最后一级的元素.

This version will not return parent elements that contains the elements having texts, only the last level elements.

可能不是最快的,但在StackOverflow主页上效果很好:)

May not be the fastest but works quite well on StackOverflow homepage :)

这篇关于使用jQuery查找文本元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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