简体   繁体   English

wrapAll()只处理第一个元素?

[英]wrapAll() only working on the first element?

I'm using this script to wrap two divs: 我正在使用这个脚本来包装两个div:

jQuery: jQuery的:

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

HTML: 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>

The weird thing is that this script only works on the first link and all others are being ignored. 奇怪的是,这个脚本只适用于第一个链接而其他所有链接都被忽略。

Any ideas what I'm doing wrong? 我有什么想法我做错了吗?

That's because you've given them all the same ID ( never use the same ID twice on a page). 那是因为你给了他们所有相同的ID( 从不在页面上使用相同的ID两次)。 Change it to class or give each link a unique ID. 将其更改为类或为每个链接指定唯一ID。

Here's an example using a common class on the links: 以下是使用链接上的公共类的示例:

jQuery: jQuery的:

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

HTML: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM