当在contenteditable按钮中键入空格时,Chrome会点击onClick [英] Chrome firing onClick when space typed in contenteditable button

查看:380
本文介绍了当在contenteditable按钮中键入空格时,Chrome会点击onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 contenteditable = true 的按钮。我可以编辑文本,但我无法在Chrome中输入空格。当我点击空格键时,Chrome会在按钮上触发onClick事件。然而,Safari会像您期望的那样插入一个空格。

I have a button with contenteditable=true. I can edit the text just fine, but I cannot type spaces in Chrome. When I hit spacebar, Chrome instead fires an onClick event on the button. Safari, however, inserts a space just as you would expect.

使用此代码,在编辑按钮时尝试键入空格会触发Chrome中的警报。

With this code, trying to type a space while editing the button triggers the alert in Chrome.

<button 
    contenteditable="true" 
    onclick="alert('wtf?');">Edit me!</button>

摆弄此代码: http://jsfiddle.net/Jrt8w/1/

为什么Chrome假装是点击事件?我怎样才能说服它插入空格?

Why is Chrome pretending that was a click event? And how can I convince it to instead insert a space?

推荐答案

event.preventDefault()

是什么救了我。在keyPress函数中有一个event.preventDefault,不应该调用onClick。

is what saved me. In the keyPress function have an event.preventDefault and the onClick should not be called.

这篇关于当在contenteditable按钮中键入空格时,Chrome会点击onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆