Rumah  >  Artikel  >  hujung hadapan web  >  jQuery is()函数用法3例_jquery

jQuery is()函数用法3例_jquery

WBOY
WBOYasal
2016-05-16 16:49:391106semak imbas

jQuery提供了is()方法可以很方便的判断元素是否可见,是否隐藏,是否选中。

一、判断元素是否隐藏

如下html中的div元素是隐藏的:

复制代码 代码如下:





   


   


二、判断checkbox是否选中

jquery中可以通过xx.is(':checked')判断checkbox,radiobutton是否是选中状态,如下测试html

复制代码 代码如下:






    chkChecked
    chkNoChecked



   


三、判断是否使用了某个样式

复制代码 代码如下:



 
  is函数介绍
 
  //注意 这里使用的是jQuery官方的脚步库


 
  //注意这里出现了第一个div
//注意这里出现了第2个div
//注意这里出现了第3个div
//注意这里出现了第4个div


Peter
//注意这里出现了第5个div
//注意这里出现了第6个div

 


<script><BR> $("div").one('click', function () { //$("div").one 代表对div元素附加一个事件,<BR>//还能附加多个事件 譬如click或者mouseout时同时执行一些事情<BR> if ($(this).is(":first-child")) { //is函数发挥作用了,is(":first-child") 代表 <BR> //判断此div是否第一个出现的div<BR> $("p").text("It's the first div."); //text和html区别在于是否支持html标记<BR> // 此时你在里面写个 alert是不会执行的<BR> } else if ($(this).is(".blue,.red")) { //判断该div是否 具有blue或者red的 class<BR> $("p").text("这是个蓝色或者红色的div");<BR> } else if ($(this).is(":contains('Peter')")) { //判断div中是否存在Peter这个词<BR> $("p").text("It's Peter!");<BR> } else {<BR> $("p").html("It's nothing <em>special.");<BR> }<BR> $("p").hide().slideDown("slow"); //这是一个动画效果。慢慢展现p的内容<BR> $(this).css({"border-style": "inset", cursor:"default"});<BR> });<BR></script>


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn