您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

复选框更改和单击事件之间的jQuery区别

复选框更改和单击事件之间的jQuery区别

根据W3C,该onclick事件是由键盘出于可访问性目的触发的:

为了给不使用鼠标的用户提供更好的用户体验,已经开发了浏览器来触发onclick事件,即使使用键盘进行 单击 也是如此。

因此,click即使使用键盘的空格键单击了复选框,jQuery的事件也会触发。change,显然,每次复选框状态更改时都会触发。

复选框恰好是changeclick可互换的特殊情况,因为您不能在change没有触发的情况下触发事件click

当然,此规则的例外情况是您是否要使用javascript手动更改复选框,例如:

/* this would check the check@R_900_2419@ without firing either 'change' or 'click' */
$('#someCheck@R_900_2419@').prop('checked',true);

/* this would fire 'change', but not 'click'. Note, however, that this
   does not change the check@R_900_2419@, as 'change()' is only the function that
   is fired when the check@R_900_2419@ changes, it is not the function that
   does the changing  */
$('#someCheck@R_900_2419@').trigger('change');

/* this would fire 'click', which by default change state of check@R_900_2419@ and automatically triggers 'change' */
$('#someCheck@R_900_2419@').trigger('click');

希望这可以帮助。

JS 2022/1/1 18:15:58 有539人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶