KAXU 发表于 2013-2-7 15:15:05

jquery 遍历 is(expr)

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’
页: [1]
查看完整版本: jquery 遍历 is(expr)