2013-09-24 42 views
1

爲什麼Jquery的.next()函數返回下一個選擇的項目時,這些項目由<br>標記分隔?jquery .next()跳過由<br>分隔的選定元素標籤

下面是從jQueryUI的網站示範區採取了一些代碼:

<!doctype html> 

<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>jQuery UI Button - Icons</title> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css" /> 
    <script> 
    $(function() { 
    $("button:first").button({ 
     icons: { 
     primary: "ui-icon-locked" 
     }, 
     text: false 
    }).next().button({ 
     icons: { 
     primary: "ui-icon-locked" 
     } 
    }).next().button({ 
     icons: { 
     primary: "ui-icon-gear", 
     secondary: "ui-icon-triangle-1-s" 
     } 
    }).next().button({ 
     icons: { 
     primary: "ui-icon-gear", 
     secondary: "ui-icon-triangle-1-s" 
     }, 
     text: false 
    }); 
    }); 
    </script> 
</head> 
<body> 

<button>Button with icon only</button> 
<button>Button with icon on the left</button> 
<button>Button with two icons</button> 
<button>Button with two icons and no text</button> 


</body> 
</html> 

這導致像顯示:

Correct display

但是,如果我的第一個元素後插入一個<br>標籤,那麼會跳過下一個.button()調用,從而導致以下顯示:

Incorrect display

第二個.next()。按鈕(...「ui-icon-locked」...)函數似乎被跳過,其餘的.next()。button()接一個。

我可以通過使用ID來解決這個問題,創建一個buttonset和樣式它,我敢肯定還有其他的方法,但是有什麼關於JQuery選擇和<br>標籤,我在這裏失蹤?

+2

因爲''
** **是下一個項目。給這個方法一個選擇器也不會改變那個。 –

回答

3

.next()總是返回下一個元素。傳遞參數給這個函數只是一個「條件」。如果條件不符合,jQuery對象將是空的。

您可以使用.nextAll():first對付這個:

$('button:first').button(...).nextAll('button:first') //and on and on 
+0

感謝您提供明確的答案並提供解決方案。我可以看到現在我是如何誤讀.next()文檔的。 – bmacnaughton