jQuery-查找仅包含文本而没有其他任何html标记的元素 [英] jquery - find element that only has text and not any other html tag

查看:107
本文介绍了jQuery-查找仅包含文本而没有其他任何html标记的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过jquery检查锚元素中是否只有文本,而没有其他任何标签(img,b)或其他任何东西.

I need to check with jquery that a anchor element only has text in it and not any other tag (img, b) or any thing else.

<a href="">TV</a>

应该找到,但是:

<a href=""><img /></a>

或:

<a href=""><span>TV</span></a>

或其他任何HTML标记,都找不到.

or any other HTML tag, shouldn't be found.

我该怎么做?

谢谢.

推荐答案

我们可以使用 filter() 函数删除具有子元素的元素(使用 children() 方法进行检查).

We can use the filter() function to remove elements which have children (checked using the children() method).

var emptyAs = $('a').filter(function () {
    return $(this).children().length == 0;
});

您也可以将 :not()选择器

You could also use the :not() selector combined with the :has() selector;

var moreEmptyAs = $('a:not(:has(*))');

您可以在下面的JSFiddle中看到这两种方法的工作; http://jsfiddle.net/JD67U/

You can see both of these working in the following JSFiddle; http://jsfiddle.net/JD67U/

这篇关于jQuery-查找仅包含文本而没有其他任何html标记的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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