2013-03-24 115 views
1

我使用這個腳本包兩個div:wrapAll()僅適用於第一個元素?

的jQuery:

$("#wrapcb").click(function(){ 
    $('#cboxOverlay, #colorbox').wrapAll('<div class="wrapcolorbox">'); 
}); 

HTML:

<span><a id="wrapcb" href="http://www.example.com/one">First link</a></span> 
<span><a id="wrapcb" href="http://www.example.com/two">Second link</a></span> 
<span><a id="wrapcb" href="http://www.example.com/three">Third link</a></span> 

奇怪的是,這個腳本只在作品上第一個環節和其他所有人都被忽略。

任何想法我做錯了什麼?

回答

4

這是因爲你給他們所有相同的ID(never在頁面上使用相同的ID兩次)。將其更改爲課程或爲每個鏈接指定一個唯一的ID。

下面是使用上的鏈接一個共同的類的例子:

的jQuery:

$(".wrapcb").click(function(){ 
    $('#cboxOverlay, #colorbox').wrapAll('<div class="wrapcolorbox">'); 
}); 

HTML:

<span><a class="wrapcb" href="http://www.example.com/one">First link</a></span> 
<span><a class="wrapcb" href="http://www.example.com/two">Second link</a></span> 
<span><a class="wrapcb" href="http://www.example.com/three">Third link</a></span> 
+0

感謝,做工精細現在:) – Kev89 2013-03-24 23:05:54

相關問題