jQuery API provides traversing methods to filter out the matched elements. We will discuss some filtering methods in this post, jQuery has(), not() and filter().

jQuery API提供了遍历方法来过滤出匹配的元素。 我们将在本文中讨论jQuery has()not()filter()一些过滤方法。

jQuery has() (jQuery has())

The has() method returns all the elements which matches at least one of the elements passed to this method. The has() method accepts a mandatory parameter which reduces the matched set of elements..

has()方法返回与传递给此方法的至少一个元素匹配的所有元素。 has()方法接受一个强制性参数,该参数会减少匹配的元素集。

Here is the general syntax for using has():

这是使用has()的一般语法:

  • selector.has(element)selector.has( element

element could be any selector expression to match against or any DOM element. You can pass multiple comma separated elements this methods like $(“div”).has(“p,span”). Here has() method return all the div elements which contains any of the elements, that is p or span element.

元素可以是与之匹配的任何选择器表达式,也可以是任何DOM元素。 您可以使用$(“ div”)。has(“ p,span”)这样的方法传递多个逗号分隔的元素。 has()方法将返回所有包含任何元素的div元素,即p或span元素。

jQuery has()示例 (jQuery has() example)

Following example demonstrates the jQuery has() usage.

以下示例演示了jQuery has()的用法。

<!doctype html>
<html>
<head>
<title>jQuery Traversing has</title>
<style>div{display: block;border: 3px solid grey;color: black;padding: 5px;margin: 25px;width:250px;text-align:center;color:red;}p{color: green;text-align:center;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body><h2>jQuery has() Demo</h2>
<div><b>This div has a p element</b>
<p><b>p - I am inside div</b></p>
</div>
<div><b>I'm not yellow because I don't have a p element</b></div><script>$(document).ready(function(){$("div").has("p").css("background-color","yellow");});
</script>
</body>
</html>

In this example, we have two div elements. We are going to check whether the div elements have a p element using jQuery has().
$("div").has("p") checks whether the div has a p element. The has() method traverses the DOM tree and finds two div elements but only one has the p. Therefore, the div which contains p will be filled with yellow.
In this example you can see that the has() method filters out the div elements using the selector passed to this method, that is a p element. Below is the output produced by above HTML page.

在此示例中,我们有两个div元素。 我们将使用jQuery has()检查div元素是否具有ap元素。
$("div").has("p")检查div是否具有ap元素。 has()方法遍历DOM树并找到两个div元素,但只有一个具有p。 因此,包含p的div将用黄色填充。
在此示例中,您可以看到has()方法使用传递给此方法的选择器ap元素过滤了div元素。 以下是以上HTML页面产生的输出。

Now we can look in to jQuery not(), another useful filtering method .

现在我们可以看看jQuery not() ,这是另一种有用的过滤方法。

jQuery not() (jQuery not())

The not() method filters out all the elements which matches the specified selector from the matched set of elements.

not()方法从匹配的元素集中过滤出所有与指定选择器匹配的元素。

Here is the general syntax for using not():

这是使用not()的一般语法:

  • selector.not(element)selector.not( element

element could be a string containing the selector expression, a function, any DOM element or any existing jQuery object.

元素可以是包含选择器表达式,函数,任何DOM元素或任何现有jQuery对象的字符串。

jQuery not()示例 (jQuery not() example)

Following example demonstrates the jQuery not() usage

以下示例演示了jQuery not()的用法

<!doctype html>
<html>
<head>
<title>jQuery traversing not</title>
<style>div {width: 50px;height: 50px;margin: 10px;float: left;background: yellow;border: 2px solid white;}.green {background: green;}
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div></div>
<div class="green"></div>
<div></div><script>$( "div" ).not( ".green" ).css( "border-color", "red" );
</script></body>
</html>

In this example, we have three div elements and two of them is defined with CSS style class="green".
$( "div" ).not( ".green" ).css( "border-color", "red" ); This line of code filters out the div elements which does not have class="green" and changes the border color to red.

在此示例中,我们有三个div元素,其中两个是使用CSS样式class="green"
$( "div" ).not( ".green" ).css( "border-color", "red" ); 这行代码过滤掉没有class="green"的div元素,并将边框颜色更改为红色。

Now we can look in to jQuery filter(), another useful filtering method .

现在我们可以看一下jQuery filter(),这是另一种有用的过滤方法。

jQuery filter() (jQuery filter())

The filter() method removes all the elements from the matched set of elements that do not match the specified selectors.

filter()方法从匹配的元素集中删除与指定选择器不匹配的所有元素。

Here is the general syntax for using filter():

这是使用filter()的一般语法:

  • selector.filter(element)选择器。过滤器( 元素

element could be a string containing the selector expression, a function, any DOM element or any existing jQuery object.

元素可以是包含选择器表达式,函数,任何DOM元素或任何现有jQuery对象的字符串。

jQuery filter()示例 (jQuery filter() example)

Following example demonstrates the jQuery filter() usage

以下示例演示了jQuery filter()的用法

<!doctype html>
<html>
<head>
<title>jQuery traversing filter</title>
<style>div,.newDivClass{width: 50px;height: 50px;margin: 10px;float: left;background: yellow;border: 2px solid white;}
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div></div>
<div class="newDivClass"></div>
<div></div><script>$( "div" ).css( "background", "yellow" ).filter( ".newDivClass" ).css( "border-color", "red" );
</script></body>
</html>

In this example, we have three div elements and one of them is defined with CSS style class="newDivClass".
First we change the back ground color of all the div elements to yellow and changes the border color of the div element which has clas==”newDivClass” using filter() method.

在此示例中,我们有三个div元素,其中一个元素是使用CSS样式class="newDivClass"
首先,我们使用filter()方法将所有div元素的背景色更改为黄色,并更改具有clas ==“ newDivClass”的div元素的边框颜色。

I hope you have understood the use of these filtering methods in jQuery. We will discuss other methods in the coming posts.

希望您了解jQuery中这些过滤方法的用法。 我们将在以后的文章中讨论其他方法。

翻译自: https://www.journaldev.com/5385/jquery-has-not-filter-functions-example

jQuery has(),not()和filter()函数示例相关推荐

  1. jquery 遍历无限极树_jQuery parent()和children()树遍历函数示例

    jquery 遍历无限极树 jQuery provides a lot of tree traversal functions that we can use to get the parent, c ...

  2. Java程序员从笨鸟到菜鸟之(八十七)跟我学jquery(三)jquery动态创建元素和常用函数示例

    在上面两篇博客中列举了太多的API相信大家看着眼晕. 不过这些基础还必须要讲, 基础要扎实.其实对于这些列表大家可以跳过, 等以后用到时再回头看或者查询官方的API说明.在本博客中就给大家讲解一下这些 ...

  3. python filter函数 字符串_Python数组条件过滤filter函数使用示例

    使用filter函数,实现一个条件判断函数即可. 比如想过滤掉字符串数组中某个敏感词,示范代码如下: #filter out some unwanted tags def passed(item): ...

  4. jQuery - Traversing遍历器 - filter过滤

    jQuery - Traversing遍历器 - filter过滤 [Traversing/filter文档] http://docs.jquery.com/Traversing/filter [示例 ...

  5. jQuery ready(fn) 页面载入函数

    ready(fn)当DOM载入就绪可以查询及操纵时绑定一个要执行的函数.这是事件模块中最重要的一个函数,因为它可以极大地提高web应用程序的响应速度.         简单地说,这个方法纯粹是对向wi ...

  6. python filter()函数(与map()、reduce()函数用法类似,根据给定函数判断列表元素是True还是False,返回剔除元素后的元素列表)

    filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表. 该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 ...

  7. filter函数的用法_这几个超牛函数,你的Excel里有没有?

    小伙伴们好啊,今天和大家分享几个Office 365中新函数的典型用法,有了这些新函数,让原本复杂的计算变得越来越简单.1.数据转置如下图所示,为了便于打印,要将A列中的姓名,转换为多行多列.D6单元 ...

  8. Python reduce / map / filter 函数区别 - Python零基础入门教程

    目录 一.map 函数 二.reduce 函数 三.filter 函数 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 Pyth ...

  9. es6 filter函数的用法_Python 函数式编程指北,不只是面向对象哦!超级详细!

    Python 函数式编程指北, 不只是面向对象哦 了解在Python中如何使用 lambda, map, filter 和 reduce 函数来转换数据结构 Photo by Markus Spisk ...

最新文章

  1. 用Python分析了1980~2015年考研英语真题词汇,原来考研英语应该这样考!
  2. python 输入文件名查找_Python实现的根据文件名查找数据文件功能示例
  3. Toad Oracle 本地/远程数据库导入/导出 数据库备份
  4. 读取javascript脚本的正则表达式
  5. Wireshark使用学习
  6. python升级第三方库,Python第三方库的卸载_安装_更新
  7. docker-compose单机容器编排工具
  8. java实现向有序数组中插入一个元素
  9. 理解交换机通过逆向自学习算法建立地址转发表的过程_交换机与 VLAN 到底是怎么来的...
  10. 改变程序设计、图灵奖得主、美国第一位计算机科学女博士,程序媛进击史
  11. 模块化无人机,不仅配置高,还颜值爆表
  12. 最小二乘法2阶拟合数据
  13. MATLAB谐波均值滤波
  14. Linux AHCI驱动分析之设备初始化
  15. 苹果App Store商店中国区如何改为美国区
  16. 小米手机获取root权限最常见问题
  17. js实现京东快递单号查询效果
  18. 打听同事工资,我被离职了
  19. polplayer下载网址和polplayer直播源
  20. 1004_Median

热门文章

  1. 磨刀不误砍柴工,使用visual studio之前应该先了解这些...
  2. IOS ViewController 生命周期
  3. chm文件的中文显示乱码问题解决
  4. UI布局引擎Layout 之 QGraphicsLinearLayout
  5. WPF--ContextMenu绑定命令的一个问题
  6. Entity Framework 与 面向对象
  7. nginx 优化(收藏)
  8. 播放失败,请在设置中切换输出设备(后续)
  9. 带你玩转JavaWeb开发之四 -如何用JS做登录注册页面校验
  10. 在Fedora上搭建GTK+的开发环境