使用 jQuery 查找带有文本的元素

发布于 2024-07-25 04:49:21 字数 210 浏览 7 评论 0原文

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

<p>some string</p>.  

我不想获取字符串,我希望数组项成为元素(在示例中,将是p 节点)。 我事先不知道字符串是什么,所以我无法查找匹配的字符串值。 我也不希望空文本节点出现在数组中。

谢谢!

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

<p>some string</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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寻找我们的幸福 2024-08-01 04:49:21
$("#my_div *").filter(function()
{
     var $this = $(this);
     return $this.children().length == 0 && $.trim($this.text()).length > 0;
})

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

可能不是最快的,但在 StackOverflow 主页上运行得很好:)

$("#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.

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

ヤ经典坏疍 2024-08-01 04:49:21

自定义选择器可能对您的情况有所帮助:

jQuery.expr[':'].hasText = function(element, index) {
     // if there is only one child, and it is a text node
     if (element.childNodes.length == 1 && element.firstChild.nodeType == 3) {
        return jQuery.trim(element.innerHTML).length > 0;
     }
     return false;
};

之后,您可以简单地执行以下操作:

$('#someDiv :hasText') // will contain all elements with text nodes (jQuery object)
$('#someDiv :hasText').get() // will return a regular array of plain DOM objects

我假设您只是尝试选择内部仅包含文本的元素。

A custom selector could be helpful in your case:

jQuery.expr[':'].hasText = function(element, index) {
     // if there is only one child, and it is a text node
     if (element.childNodes.length == 1 && element.firstChild.nodeType == 3) {
        return jQuery.trim(element.innerHTML).length > 0;
     }
     return false;
};

After that, you can simply do this:

$('#someDiv :hasText') // will contain all elements with text nodes (jQuery object)
$('#someDiv :hasText').get() // will return a regular array of plain DOM objects

I am assuming that you are only trying to select elements that have ONLY text inside of them.

始终不够爱げ你 2024-08-01 04:49:21

您可以使用 not 和空选择器来获取非空元素,同时可以使用 get 转换为数组。

$("#theDiv > :not(:empty)").get();

上面的选择器获取“theDiv”的所有非空子元素(即它们要么有child 或 text),然后将匹配的集合转换为数组。

如果您只想要其中包含文本的元素,这应该可以...

$("#theDiv > :not(:empty, :has(*))").get();

要摆脱包含空格的元素,您可以使用过滤器

$("#theDiv > :not(:has(*))").filter(function() { 
                 return $.trim(this.innerHTML).length > 0;
         }).get();

You can make use of not and the empty selectors to get the non-empty elements while converting to an array can be achieved using get

$("#theDiv > :not(:empty)").get();

The above selector gets all the children elements of "theDiv" and that aren't empty (i.e. they either have children or text) and then converts the matched set to an array.

If you want only elements that have text inside them, this should work...

$("#theDiv > :not(:empty, :has(*))").get();

To get rid of elements that have whitespace you can make use of filter

$("#theDiv > :not(:has(*))").filter(function() { 
                 return $.trim(this.innerHTML).length > 0;
         }).get();
度的依靠╰つ 2024-08-01 04:49:21

您可以循环遍历 children 并获取具有 .text()!= ""

You could cycle through the children and grab everything that has a .text() value != ""

腹黑女流氓 2024-08-01 04:49:21
var array = [];
var divSelector = "div.mine";

$(divSelector).contents().each(function()
{
   // If not an element, go to next node.
   if (this.nodeType != 1) return true;       

   var element = $(this);
   if ($.trim(element.text()) != "")
     array.push(element);
});

array 是其中包含一些文本的元素数组。

var array = [];
var divSelector = "div.mine";

$(divSelector).contents().each(function()
{
   // If not an element, go to next node.
   if (this.nodeType != 1) return true;       

   var element = $(this);
   if ($.trim(element.text()) != "")
     array.push(element);
});

array is the array of elements that have some text in them.

人│生佛魔见 2024-08-01 04:49:21
 $(function() {
        var array = new Array();
        $("#testDiv *").each(function(x, el) {

            if ($.trim($(el).text()) != '' ) {
                array.push(el);
            }
        });
        alert(array.length);
    });
 $(function() {
        var array = new Array();
        $("#testDiv *").each(function(x, el) {

            if ($.trim($(el).text()) != '' ) {
                array.push(el);
            }
        });
        alert(array.length);
    });
恋竹姑娘 2024-08-01 04:49:21

d 是要在其下查找内容的 div
v 是一个空数组
我你必须从 0 开始。

使用 $.trim 是为了避免得到只有空白的节点。

$("*",d).filter( function() { 
     return $.trim($(this).text()) != ""
  } ).each( function() { 
     v[i] = $(this).text(); 
     i++; 
  } );

人们还可以使用 v.push($(this))...这是我完全忘记的事情。

d is the div under which you want to find things
v is an empty array
i you have to start out at 0.

The $.trim is used so that you don't get nodes that are just whitespace.

$("*",d).filter( function() { 
     return $.trim($(this).text()) != ""
  } ).each( function() { 
     v[i] = $(this).text(); 
     i++; 
  } );

One can also use v.push($(this))...which is something that totally slipped my mind.

朮生 2024-08-01 04:49:21

使用 :contains 选择器:

var matches = new Array();
$('#start_point *:contains(' + text + ')').each(function(i, item) {
 matches.push( item );
}

Use the :contains selector:

var matches = new Array();
$('#start_point *:contains(' + text + ')').each(function(i, item) {
 matches.push( item );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文