jQuery 遍历 - is() 方法

Javascript 1982 0 2011-09-05

jQuery 遍历 - is() 方法

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/jquery/jquery.js"></script>
</head>

<body>
<ul>
  <li><strong>list</strong> item 1 - one strong tag</li>
  <li><strong>list</strong> item <strong>2</strong> -
    two <span>strong tags</span></li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>

<script>
$("li").click(function() {
  var $li = $(this),
    isWithTwo = $li.is(function() {
      return $('strong', this).length === 2;
    });
  if ( isWithTwo ) {
    $li.css("background-color", "green");
  } else {
    $li.css("background-color", "red");
  }
});
</script>

</body>
</html>

根据教程,点击li后的效果应该是:

但是我在DW里面写出来一样的代码效果居然是这样的:

问:为什么会是这样呢?

上一篇:jQuery 遍历 - andSelf() 方法

下一篇:jQuery 遍历 - nextUntil() 方法

讨论数量:0

请先登录再发表讨论。 2024-04-19

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链