closest返回一個或0個結果?
下述代碼第一個按鈕點擊后返回了兩個結果,不是應該一個或0個結果么?
<!DOCTYPE html>
<html>
<head>
? ? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
? ? <title></title>
? ? <link rel="stylesheet" href="imooc.css" type="text/css">
? ? <style>
? ? .left {
? ? ? ? width: auto;
? ? ? ? height: 200px;
? ? }
? ??
? ? .left div {
? ? ? ? width: 350px;
? ? ? ? height: 150px;
? ? ? ? padding: 5px;
? ? ? ? margin: 5px;
? ? ? ? float: left;
? ? ? ? background: #bbffaa;
? ? ? ? border: 1px solid #ccc;
? ? }
? ??
? ? span {
? ? ? ? color: blue;
? ? }
? ? </style>
? ? <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
</head>
<body>
? ? <h2>closest方法()</h2>
? ? <div class="left first-div">
? ? <div class="div">
? ? ? ? <ul class="level-2">
? ? ? ? ? ? <li class="item-a">A</li>
? ? ? ? ? ? <li class="item-1">B
? ? ? ? ? ? ? ? <ul class="level-3">
? ? ? ? ? ? ? ? ? ? <li class="item-1">1</li>
? ? ? ? ? ? ? ? ? ? <li class="item-2">2</li>
? ? ? ? ? ? ? ? ? ? <li class="item-3">3</li>
? ? ? ? ? ? ? ? </ul>
? ? ? ? ? ? </li>
? ? ? ? ? ? <li class="item-c">C</li>
? ? ? ? </ul>
? ? </div>
? ? </div>
? ? <br/>
? ? <button>點擊:closest傳遞選擇器 </button>
? ? <button>點擊:closest傳遞一個元素對象</button>
? ? <script type="text/javascript">
? ? $("button:first").click(function() {
? ? ? ? ? ? $("li.item-1").closest($("li.item-1"))
? ? ? ? ? ? .css('border', '1px solid red');
? ? })
? ? </script>
? ? <script type="text/javascript">
? ? $("button:last").click(function() {
? ? ? ? var itemB = $('.item-b')
? ? ? ? $('li.item-1')
? ? ? ? ? ? .closest(itemB)
? ? ? ? ? ? .css('border', '1px solid blue');
? ? })
? ? </script>
</body>
</html>
2017-05-10
jquery對象里面有兩個DOM對象
2017-05-15
close()對象返回一個或者0 個對象是指:
?$(selector) .closest(selector)篩選的每一個選定的jQuery對象其closest()對象結果只有一個,當?$(selector) 篩選結果為多個時,其結果集中的每個jQuery對象closest返回1個或0個,則總體就是多個了;