两个按钮和DIV具有相同的“onClick”代码,但在将execCommand似乎只能在按钮上工作。有什么办法可以使它在按div时工作吗?为什么不document.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>
我猜它不适用于div,因为你失去了焦点和选择。用按钮保持焦点。 – DeadAlready