wrapAll()只在第一个元素上工作? [英] wrapAll() only working on the first element?

查看:75
本文介绍了wrapAll()只在第一个元素上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个脚本来封装两个div:
$ b

jQuery:

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

HTML:

 < span>< a id =wrapcbhref =http://www.example.com/one>第一个连结< / a><跨度> 
< span>< a id =wrapcbhref =http://www.example.com/two>第二个连结< / a>< / span>
< span>< a id =wrapcbhref =http://www.example.com/three>第三方连结< / a>< / span>

奇怪的是,这个脚本只能在第一个链接上工作,其他的都被忽略。



任何想法我做错了什么?

解决方案

因为您已向他们提供了所有相同的ID( 从不 在页面上使用相同的ID两次)。将其更改为课程或为每个链接指定一个唯一的ID。



以下是一个在链接上使用普通类的示例:

jQuery:

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

HTML:

 < span>< a class =wrapcbhref =http://www.example.com/one>第一个连结< / a><跨度> 
< span>< a class =wrapcbhref =http://www.example.com/two>第二个连结< / a>< / span>
< span>< a class =wrapcbhref =http://www.example.com/three>第三方连结< / a>< / span>


I'm using this script to wrap two divs:

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>

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). Change it to class or give each link a unique ID.

Here's an example using a common class on the links:

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>

这篇关于wrapAll()只在第一个元素上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆