单击div时,为什么document.execCommand不起作用? [英] Why doesn't the document.execCommand work when I click on a div?

查看:1296
本文介绍了单击div时,为什么document.execCommand不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按钮和div都有相同的'onclick'代码,但execCommand似乎只对按钮有效。按下div时有什么方法可以让它工作吗?

Both the button and the div have the same 'onclick' code, yet the execCommand only seems to work on the button. Is there any way I can make it work when pressing the div?

这是我的小提琴:
http://jsfiddle.net/foreyez/ZzL8y/

<button onclick="document.execCommand('bold',false,null);">Bold</button>
<div onclick="document.execCommand('bold',false,null);" style='border:1px solid black;width:50px;'>Bold</div>

<div id='input' contenteditable='true'>
    select some of this text and then hit one of the buttons above
</div>

推荐答案

您需要阻止div上的 mousedown 事件,因为它会抢断焦点:

You need to prevent the mousedown event on your div because it steals the focus:

< a href =http://jsfiddle.net/ZzL8y/5/ =noreferrer>更新小提琴

这篇关于单击div时,为什么document.execCommand不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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