六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

jquery 遍历 is(expr)

[复制链接]

升级  67.33%

130

主题

130

主题

130

主题

举人

Rank: 3Rank: 3

积分
402
 楼主| 发表于 2013-2-7 15:15:05 | 显示全部楼层 |阅读模式
is(expr)
判断现有集合是否属于‘expr’集合中的一部分或是相等。如果是则返回true,否则返回false
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                     "http://www.w3.org/TR/html4/loose.dtd"><html><head>  <script src="http://code.jquery.com/jquery-latest.js"></script>    <script>  $(document).ready(function(){        $("div").one('click', function () {      if ($(this).is(":first-child")) {        $("p").text("It's the first div.");      } else if ($(this).is(".blue,.red")) {        $("p").text("It's a blue or red div.");      } else if ($(this).is(":contains('Peter')")) {        $("p").text("It's Peter!");      } else {        $("p").html("It's nothing <em>special</em>.");      }      $("p").hide().slideDown("slow");      $(this).css({"border-style": "inset", cursor:"default"});    });  });  </script>  <style>  div { width:60px; height:60px; margin:5px; float:left;        border:4px outset; background:green; text-align:center;         font-weight:bolder; cursor:pointer; }  .blue { background:blue; }  .red { background:red; }  span { color:white; font-size:16px; }  p { color:red; font-weight:bolder; background:yellow;       margin:3px; clear:left; display:none; }  </style></head><body>  <div></div>  <div class="blue"></div>  <div></div>  <div class="red"></div>  <div><br/><span>Peter</span></div>  <div class="blue"></div>  <p> </p></body></html> 
if ($(this).is(":first-child")) {
} else if ($(this).is(".blue,.red")) {
} else if ($(this).is(":contains('Peter')")) {
这3行分别是判断当前集合中是否为第一个子集;是否包含特殊样式‘blue’或是‘red’;是否包含文本内容‘peter’
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表