jQuery 伪造点击事件
假设我有代码:
$('#target').click(function() {
alert('Handler for .click() called.');
});
我希望页面加载后 #target
将被自动单击,但单击功能保持活动状态。
例如,用于播放和停止的相同按钮。这将启动播放方法,但如果再次按下将停止该方法。
我应该将其放入单独的方法中,还是对此类事件进行单行调用?
Lets say I have code:
$('#target').click(function() {
alert('Handler for .click() called.');
});
I want that after page is loaded #target
would be auto clicked, yet click function remain active.
For example same button for play and stop. This would launch play method but if pressed again would stop that.
Should I just trow it to separate method or if there is single line call for such event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很简单,你几乎已经掌握了......
这是一个小提琴: http://jsfiddle.net/gislikonrad/xqKBE /
It's quite simple and you almost had it...
Here's a fiddle: http://jsfiddle.net/gislikonrad/xqKBE/
我认为你应该使用
toggle()
事件(而不是与显示和隐藏的切换混淆!),因为这可以处理同一事件的多个功能。然后您可以在绑定后调用click()
来自动调用一次:< a href="http://jsfiddle.net/TUBqy/" rel="nofollow">JSFiddle 示例
I think you should the use
toggle()
event (not to be confused with toggle for showing and hiding!) as this handles multiple functions off the same event. You can then callclick()
after binding to call it once automatically:JSFiddle Example