最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to programmatically click an element with MooTools? - Stack Overflow

programmeradmin6浏览0评论

In jQuery, I've done stuff like this in the past:

$('#someCheckbox').click();

And everything works as if the user just clicked on the element normally. However the same doesn't work in MooTools:

$('someCheckbox').fireEvent('click');

The checkbox doesn't get checked, nor do any of the bound event handlers fire.

Is there a way to do this? I need the already bound "click" event handlers to fire, so just setting it's "checked" attribute isn't an option.

In jQuery, I've done stuff like this in the past:

$('#someCheckbox').click();

And everything works as if the user just clicked on the element normally. However the same doesn't work in MooTools:

$('someCheckbox').fireEvent('click');

The checkbox doesn't get checked, nor do any of the bound event handlers fire.

Is there a way to do this? I need the already bound "click" event handlers to fire, so just setting it's "checked" attribute isn't an option.

Share Improve this question edited Mar 7, 2020 at 9:48 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 3, 2010 at 21:30 adamJLevadamJLev 14k11 gold badges62 silver badges65 bronze badges 2
  • does $('someCheckbox').onclick() work? – Jack Commented Mar 3, 2010 at 21:45
  • Firefox says: element.onclick is not a function – adamJLev Commented Mar 3, 2010 at 21:57
Add a ment  | 

1 Answer 1

Reset to default 6

The click event should fire when fireEvent('click') is called. See http://mootools/shell/8bbgn/ for a demo I just set up.

The checkbox doesn't get checked, as expected. To do this, you must set the checked property to true:

$('someCheckbox').setProperty('checked', true);
发布评论

评论列表(0)

  1. 暂无评论