jQuery attr('onclick')
我试图更改 jQuery 中的“onclick”属性,但它没有改变,这是我的代码:
$('#stop').click(function() {
$('next').attr('onclick','stopMoving()');
}
我有一个 id="stop" 的元素,当用户单击它时,我想更改元素上的 onclick 属性id=“下一个”。
如果有人知道解决方案在哪里,请帮忙!
I'am trying to change "onclick" attribute in jQuery but it doesn't change, here is my code:
$('#stop').click(function() {
$('next').attr('onclick','stopMoving()');
}
I have an element with id="stop" and when user clicks on it I want to change an onclick attribute on element which has id="next".
If someone knows where is the solution please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以 jQuery 方式执行(并修复错误):
如果元素已经通过
onclick
属性附加了一个click
处理程序,则必须将其删除:更新: 正如 @Drackir 指出的,您可能还需要调用
$('#next').unbind('click');
才能删除通过 jQuery 附加的其他点击处理程序。但这是猜测。一如既往:更多信息 =>更好的答案。
Do it the jQuery way (and fix the errors):
If the element already has a
click
handler attached via theonclick
attribute, you have to remove it:Update: As @Drackir pointed out, you might also have to call
$('#next').unbind('click');
in order to remove other click handlers attached via jQuery.But this is guessing here. As always: More information => better answers.
正如@Richard 上面指出的,onClick 需要有一个大写的“C”。
As @Richard pointed out above, the onClick needs to have a capital 'C'.
最简单的方法是将 .attr() 函数更改为 javascript 函数 .setAttribute()
The easyest way is to change .attr() function to a javascript function .setAttribute()
Felix Kling 的方法会起作用(实际上比我先发制人),但我也建议使用
$('#next').die().live('click', stopMoving);
$('#next').die().live('click', stopMoving);如果多次单击元素时遇到问题和奇怪的行为,这可能是更好的方法。
Felix Kling's way will work, (actually beat me to the punch), but I was also going to suggest to use
$('#next').die().live('click', stopMoving);
this might be a better way to do it if you run into problems and strange behaviors when the element is clicked multiple times.