In this post, we will discuss how to use the jQuery is() method. This is an important filtering method used often in callback functions like event handlers.

在本文中,我们将讨论如何使用jQuery is()方法。 这是事件处理程序之类的回调函数中经常使用的重要过滤方法。

jQuery is() (jQuery is())

We use jQuery is() to check the selected element with another element, selector or any jQuery object. This method traverses along the DOM elements to find a match, which satisfies the passed parameter. It will return true if there is a match otherwise returns false.

我们使用jQuery is()与另一个元素,选择器或任何jQuery对象一起检查选定的元素。 此方法遍历DOM元素以找到匹配项,该匹配项满足传递的参数。 如果存在匹配项,它将返回true,否则返回false。

Here is the general syntax for using is();

这是使用is()的一般语法;

  • selector.is(filter)selector.is(过滤器)

filter could be string selector expression, function, elements or any existing jQuery object.

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

jQuery is()示例 (jQuery is() example)

Following example demonstrates jQuery is() method usage.

下面的示例演示jQuery is()方法的用法。

<!doctype html>
<html>
<head>
<title>jQuery traversing is() example</title>
<style>div {width: 60px;height: 60px;margin: 5px;float: left;border: 4px ;background: green;text-align: center;font-weight: bolder;cursor: pointer;}.blue {background: blue;} .red {background: red;}.grey{background: grey;}span {color: white;font-size: 16px;}p {color: red;font-weight: bolder;background: yellow;margin: 3px;clear: left;display: none;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<h2>Click on each div </h2>
<div id="sachin"><span>Sachin</span></div>
<div class="blue"><span>Ganguly</span></div>
<div><span>Dravid</span></div>
<div class="red">Srinath</div>
<div class="grey"><span>Kumble</span></div>
<div><span>Sanju</span></div>
<p>&nbsp;</p><script>
$( "div" ).on( "click", function() {if ( $( this ).is( "#sachin" ) ) {$( "p" ).text( "Best Batsman in the world" );} else if ( $( this ).is( ".blue" ) ) {$( "p" ).text( "Best Captain Ever" );} else if ( $( this ).is( ":contains('Dravid')" ) ) {$( "p" ).text( "The Wall" );} else if ( $( this ).is( ".red,.grey" ) ) {$( "p" ).html( "Clicked on Srinath or Kumble : From Karnataka" );}else {$( "p" ).html( "Young Talent" );}$( "p" ).hide().slideDown( "slow" );$( this ).addClass("yellow");
});
</script></body>
</html>

In this example, you can see how is() method is used.

在此示例中,您可以看到is()方法的用法。

$( this ).is( "#sachin" ) : Here, is() method check if the selected element’s id="sachin".

$( this ).is( "#sachin" ) :这里,is()方法检查所选元素的id="sachin"

$( this ).is( ".blue" ) : is() check if the selected element belongs to class="blue".

$( this ).is( ".blue" ) :is()检查所选元素是否属于class="blue"

$( this ).is( ":contains('Dravid')" ) : Here is() checks whether the element contains a string “Dravid”.

$( this ).is( ":contains('Dravid')" ) :这里is()检查元素是否包含字符串“ Dravid”。

$( this ).is( ".red,.grey" ) : This line of code checks whether the selected element belongs to class="red" or class="blue". The output will be same for Srinath and Kumble.

$( this ).is( ".red,.grey" ) :此代码行检查所选元素是属于class="red" or class="blue"SrinathKumble的输出将相同。

This will return true if it passes any of these conditions and display an output text with a sliding effect below the div elements. Otherwise executes the default condition. In our case, default condition executes when you click on Sanju
You can try passing function, jQuery object or any elements to the jQuery is() method.

如果通过以上任何条件,并在div元素下方显示具有滑动效果的输出文本,则返回true。 否则执行默认条件。 在本例中,当您单击Sanju时将执行默认条件
您可以尝试将函数,jQuery对象或任何元素传递给jQuery is()方法。

You can try it yourself by clicking on any of the colored div elements below.

您可以通过单击下面的任何彩色div元素来自己尝试。

演示地址

That’s all for now. We will discuss some more traversing techniques in the coming posts.

目前为止就这样了。 我们将在以后的文章中讨论更多的遍历技术。

翻译自: https://www.journaldev.com/5374/how-to-use-is-function-in-jquery

如何在jQuery中使用is()函数相关推荐

  1. 工厂好的html页面,jquery中被誉为工厂函数的是什么?

    jquery中被誉为工厂函数的是"$()".在jQuery中,无论我们使用哪种类型的选择符都需要从一个"$"符号和一对"()"开始.下面本篇 ...

  2. php中glob怎么用,如何在php中利用glob函数对文件进行遍历

    如何在php中利用glob函数对文件进行遍历 发布时间:2020-12-11 16:02:47 来源:亿速云 阅读:76 作者:Leah 如何在php中利用glob函数对文件进行遍历?相信很多没有经验 ...

  3. hive 元数据 自定义_如何在Hive中创建自定义函数UDF及如何直接通过Impala的同步元数据重用UDF的jar文件-阿里云开发者社区...

    如何在Hive中创建自定义函数UDF及使用 如何在Impala中使用Hive的自定义函数 UDF函数开发 使用Intellij工具开发Hive的UDF函数,进行编译: 1.使用Intellij工具通过 ...

  4. jquery中serialize()序列化函数

    全栈工程师开发手册 (作者:栾鹏) jquery系列教程6-AJAX全解 jquery中serialize()序列化函数 serialize()序列化,将元素转化为xx=xx&xx=xx&am ...

  5. slice()如何在jQuery中工作

    Earlier we looked how to get an element with specific index in jQuery, but sometimes we want to get ...

  6. jquery 获取同级元素_如何在jQuery中获取元素的同级

    jquery 获取同级元素 In this post, we will discuss how to get the siblings of an HTML element in jQuery. jQ ...

  7. linux之getcwd函数解析,如何在Linux 中使用getcwd()函数

    如何在Linux 中使用getcwd()函数 发布时间:2021-03-15 17:35:21 来源:亿速云 阅读:106 作者:Leah 这期内容当中小编将会给大家带来有关如何在Linux 中使用g ...

  8. Microsoft Excel 教程:如何在 Excel 中使用 COUNTIF 函数?

    欢迎观看 Microsoft Excel 教程,小编带大家学习 Microsoft Excel 的使用技巧,了解如何在 Excel 中使用 COUNTIF 函数. COUNTIF 是一个统计函数,用于 ...

  9. Microsoft Excel 教程:如何在 Excel 中使用 XLOOKUP 函数?

    欢迎观看 Microsoft Excel 教程,小编带大家学习 Microsoft Excel 的使用技巧,了解如何在 Excel 中使用 XLOOKUP 函数. 需要逐行在表或区域中查找内容时,可在 ...

  10. 抓取html表单验证码,如何在jquery中利用表单获取短信验证码

    如何在jquery中利用表单获取短信验证码 发布时间:2021-02-18 15:42:54 来源:亿速云 阅读:88 作者:Leah 这期内容当中小编将会给大家带来有关如何在jquery中利用表单获 ...

最新文章

  1. 动态卷积系列工作的基础:CondCov按需定制的卷积权重
  2. git.exe在哪里?
  3. mysql中pager命令妙用
  4. oracle数据库怎么导出dat文件_论将数据从数据库导出为dat格式.docx
  5. Android View体系(三)属性动画
  6. mysql 5.5.15_mysql5.5.15配置主从数据库
  7. Service Mesh所应对的8项挑战
  8. python redis订阅_Python -- Redis 发布订阅
  9. iOS原生实现二维码拉近放大
  10. MongoDB游标操作(4)
  11. My97DatePicker JS时间控件 当前日期前后不能选
  12. ntohs, ntohl, htons,htonl的比较
  13. C语言 数组排序 打乱 查找
  14. java阿里云文本反垃圾sdk调用详解
  15. linux docer 安装redis
  16. Xunity.autotranslator机翻unity引擎的游戏
  17. 如何删除设备和驱动器中的百度网盘
  18. 我的CSDN博客规划说明
  19. 黑科技编程开发,利用Python使图片完美去除水印
  20. Super Hide IP 3.4.7.8允许您以匿名方式进行网上冲浪、 保持隐藏您的 IP 地址

热门文章

  1. [转]memcached+magent实现memcached集群
  2. cxGrid 怎样才能让不自动换行 WordWrap:=false
  3. 处理百万级以上的数据处理
  4. FreeBSD下nginx并支持php配置详解
  5. 在 Linux 上部署 Django 应用,nginx+gunicorn+supervisor
  6. [转载] tensorflow如何微调时如何只训练后两层_XLNet只存在于论文?都替你封装好了还不来用!...
  7. ABAP 读取FTP文件
  8. 若不能细水长流地书写内心的温柔,那轰轰烈烈的一幕一幕不过是日后回忆自己爱无能的证据罢了。...
  9. js tooltip之wz_tooltip使用方法和参数备忘
  10. 记一次成功的sql注入入侵检测附带sql性能优化