【发布时间】:2012-09-21 06:04:42
【问题描述】:
按钮和 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>
【问题讨论】:
-
我猜它不适用于 div,因为您失去了焦点和选择。使用按钮保持焦点。
标签: javascript html