单击div时,为什么document.execCommand不起作用?

for*_*yez 6 javascript html5

按钮和div都有相同的'onclick'代码,但execCommand似乎只对按钮有效.按下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>
Run Code Online (Sandbox Code Playgroud)

Kon*_*nev 23

您需要阻止mousedowndiv上的事件,因为它会窃取焦点:

更新了小提琴