gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:33:15 25 4
gpt4 key购买 nike

我正在使用这个脚本来包装两个 div:

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>

奇怪的是,这个脚本只对第一个链接起作用,其他的都被忽略了。

知道我做错了什么吗?

最佳答案

那是因为您为它们提供了相同的 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>

关于javascript - wrapAll() 只处理第一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com