javascript - Why textarea with contenteditable value true don't register keydown event -
it confuse me.
code below shows, when press "ctrl + b" in div, font weight turn bolder, while, won't happen in textarea.
question based on comments in marcus ekwall's answer rendering html inside textarea. can't add comment, ask here.
div, textarea { width: 100px; height: 100px; border: 1px solid; padding: 5px; } textarea { resize: none; }
<div contenteditable="true"></div> <textarea contenteditable="true" placeholder="textarea"></textarea>
in textarea doesn't work because can not support html tags when run document.execcommand("bold") add <b>fdsfsdfds</b>
in selected text
here example using jquery (updated)
$("#editor").keypress("c",function(e){ if(e.ctrlkey) { document.execcommand('bold', false, null); } })
#editor { width: 200px; height: 200px; border: 1px solid; padding: 5px; resize: none; border: 1px solid black; word-wrap: break-word; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="editor" contenteditable="true"></div>
please see post keypress jquery: keypress, ctrl+c (or combo that)
more examples document.execcommand http://codepen.io/netsi1964/pen/qbllgw http://codepen.io/netsi1964/pen/qbllgw
Comments
Post a Comment