[英]Why doesn't the document.execCommand work when I click on a div?
Both the button and the div have the same 'onclick' code, yet the execCommand only seems to work on the button. 按钮和div都有相同的'onclick'代码,但execCommand似乎只对按钮有效。 Is there any way I can make it work when pressing the div? 按下div时,有什么方法可以使它工作吗?
Here's my fiddle: http://jsfiddle.net/foreyez/ZzL8y/ 这是我的小提琴: 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>
You need to prevent the mousedown
event on your div because it steals the focus: 您需要阻止div上的mousedown
事件,因为它会窃取焦点:
See this fiddle :- http://jsfiddle.net/ZzL8y/4/ 看到这个小提琴: - http://jsfiddle.net/ZzL8y/4/
This is not the complete answer but these links might help you:- 这不是完整的答案,但这些链接可能对您有所帮助: -
1> set execcommand just for a div 1> 为一个div设置execcommand
2> select all text in contenteditable div when it focus/click 2> 在焦点/单击时选择contenteditable div中的所有文本
For Firefox, it's necessary to set contenteditable = true before execCommand. 对于Firefox,必须在execCommand之前设置contenteditable = true。
For IE it is not necessary. 对于IE,没有必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.