简体   繁体   English

<button>元素未在 Firefox 中传播事件?</button>

[英]<button> element not propagating events in Firefox?

I'm seeing issues in Firefox 4.0.1 where event propagation does not seem to be working the way I would expect.我在 Firefox 4.0.1 中看到问题,其中事件传播似乎没有按我预期的方式工作。 Let's say I have the following HTML:假设我有以下 HTML:

<button>
    <input id='cb' type='checkbox' checked='true'>
    <span> foo</span>
</button>

and javascript:和 javascript:

$(document).ready(function() {
    $("button").click(function(e) {
        console.log('got click: button');
        return true;
    });
    $("#cb").click(function(e) {
        console.log('got click: checkbox');
        return true;
    });
});

See: http://jsfiddle.net/H7fp3/10/见: http://jsfiddle.net/H7fp3/10/

This behaves as I would expect in Chrome (12), and Safari (5.0.5):这与我在 Chrome (12) 和 Safari (5.0.5) 中所期望的一样:

  • Click on the checkbox produces: got click: checkbox , then got click: button点击复选框产生: got click: checkbox ,然后got click: button
  • Click on the button alone, produces: got click: button单独点击按钮,产生: got click: button

However, in Firefox (4.0.1) both produce got click: button and do not alter the checkbox checked attribute.但是,在 Firefox (4.0.1) 中,两者都产生got click: button并且不更改复选框选中属性。 Am I missing something here?我在这里错过了什么吗? Is Firefox propagating button click events correctly? Firefox 是否正确传播按钮单击事件?

I'd say Firefox is actually in the right here.我想说 Firefox 实际上就在这里。 According to the W3C's validation tool , <input> isn't allowed inside <button> .根据W3C 的验证工具<button>中不允许使用<input> > 。

Also, checked="true" should be checked="checked" , which might also be causing issues (or could potentially cause issues in other contexts).此外, checked="true"应为checked="checked" ,这也可能导致问题(或可能在其他情况下导致问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM