简体   繁体   English

Firefox没有在周围的div上注册click事件

[英]Firefox not registering click event on surrounding div

I have the following code: 我有以下代码:

<div data-type="nameHolder">
   <input data-type="name" disabled="">
</div>

My idea is to keep the input disabled and only when you double click on it to enable it. 我的想法是禁止输入,只有当你双击它才能启用它。 However since click events are not detected on disabled elements I'm using a holder div. 但是,由于在禁用的元素上未检测到单击事件,因此我使用了持有者div。 This works fine on Chrome, but not on Firefox. 这适用于Chrome,但不适用于Firefox。 Any idea how to fix it? 知道怎么解决吗?

since disabled elements don't fire events, you can use a div as a wrapper over your input and attach event handler to this div. 由于禁用的元素不会触发事件,因此您可以使用div作为输入的包装器并将事件处理程序附加到此div。 when you double click on wrapper div, enable the input and hide the wrapper div. 当您双击包装器div时,启用输入并隐藏包装器div。

 $('.wrapper').dblclick(function(){ $(this).prev('input').attr('disabled', false); $(this).hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-type="nameHolder"> <input data-type="name" disabled=""> <div class="wrapper" style="position:absolute; top:0;left:0;right:0;bottom:0"></div> </div> 

There is already a solution for your case in here . 已经有针对你的情况的解决方案在这里 Author proposed to place an element in front of the disabled input and catch the click on that element for crossbrowser support. 作者建议在禁用的输入前放置一个元素,并捕获该元素的点击以获得交叉浏览器支持。

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

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