如何将 jquery 事件添加到已添加的类中?
我正在尝试创建一个图片库,我使用数字而不是左/右箭头。目前,我试图让它只使用 2 个图像(ig 2 个数字),
这是 html 。 id 页面,是
<div class="grid_1 pagelink" id="page"><p><a href="">1</a></p></div>
<div class="grid_1 pagelink"><p><a href="">2</a></p></div>
页面第一次加载时突出显示的数字,下面的代码有效。所以当我点击链接 2 时,下面的代码运行良好。但是当我返回第一个链接时,我希望触发相同的代码;但是当我这样做时,页面会通过忽略下面的代码来刷新:
$('.pagelink').live('click', function(e){
e.preventDefault();
var frame = $(this).text() - 1;
var frames = 240 * frame;
// $('#gal').animate({marginLeft:'500px'},'slow');
$('#gal').animate({marginLeft: "+="+frames+'px'},'slow');
$(this).attr('id', 'page').removeClass('pagelink');
$('#page').addClass('pagelink').removeAttr('id','page');
// $('#book').animate({ left: '50' });
})
我认为 .live() 会为我做到这一点,但它不起作用。
我希望你能帮忙
谢谢
I am trying to create an image gallery, I am using numbers instead of left/right arrows. At the moment, I am trying to get it working with only 2 image (i.g 2 numbers)
this is the html . the id page, is the highlighted number
<div class="grid_1 pagelink" id="page"><p><a href="">1</a></p></div>
<div class="grid_1 pagelink"><p><a href="">2</a></p></div>
the first time the page loads, the code below works. so when I click on link 2 the the code bellow runs fine. But then I want the same code to be triggered when I click back on the first link; but when I do that, the page refreshes by ignoring the code bellow:
$('.pagelink').live('click', function(e){
e.preventDefault();
var frame = $(this).text() - 1;
var frames = 240 * frame;
// $('#gal').animate({marginLeft:'500px'},'slow');
$('#gal').animate({marginLeft: "+="+frames+'px'},'slow');
$(this).attr('id', 'page').removeClass('pagelink');
$('#page').addClass('pagelink').removeAttr('id','page');
// $('#book').animate({ left: '50' });
})
I thought that the .live() would do that for me but it is not working.
I hope you could help
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
生活应该很好。我认为你的代码中有一个错误,它可能在这里:
你到底想用这段代码完成什么?
当你点击page2时,你将div的id设置为
page
,但是现在你有2个元素的id为page,当你选择该id时,你会得到第一个(即page1) ),但您仍然从 page2 中删除了类pagelink
,换句话说,错误是在某个时候您将拥有 2 个具有相同 id 的元素(并且 id 必须是唯一的顺便说一句),所以当您使用
$('#page')
选择该 id 时,您总是会得到第一个live should work fine. i think you have a bug in your code, and its probably here:
what exactly are you trying to accomplish with this code?
when you click on page2, the you set the div's id to be
page
, but now you have 2 elements with an id of page, and when you select that id, you get the first one (ie page1), but you still remove the classpagelink
from page2in other words, the bug is that at some point you will have 2 elements with the same id (and ids must be unique btw) so when you select that id with
$('#page')
you always get the first one以前,这是因为您要删除用于映射单击事件的链接“pagelink”类。
另外,使用另一个类而不是 id(#page) 来标识#page 链接,如果 id 已经分配给其他链接,则 id 可能会出现问题。喜欢
Previous, this is because you are removing the class of link "pagelink" which were used to map the clicked event.
Also, use another class instead of id(#page) to identify the #page link, id might be problem if its already assign to other link. like