jQuery attr('onclick') [英] jQuery attr('onclick')

查看:258
本文介绍了jQuery attr('onclick')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改jQuery中的"onclick"属性,但它没有改变,这是我的代码:

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()');
}

我有一个id ="stop"的元素,当用户单击它时,我想更改id ="next"的元素的onclick属性.

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!

推荐答案

使用jQuery方法(并修复错误):

Do it the jQuery way (and fix the errors):

$('#stop').click(function() {
     $('#next').click(stopMoving);
     // ^-- missing #
});  // <-- missing );

如果元素已经通过onclick属性附加了click处理程序,则必须将其删除:

If the element already has a click handler attached via the onclick attribute, you have to remove it:

$('#next').attr('onclick', '');

更新:正如@Drackir指出的那样,您可能还必须调用$('#next').unbind('click');才能删除通过jQuery附加的其他点击处理程序.

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.

这篇关于jQuery attr('onclick')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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