Restrict carriage return on writable div
I have made a div writable by adding contentEditabl开发者_JS百科e="true". I want to restrict the text to one line and restrict users to enter multiple lines. How can i do that?
Bind a function to the keypress event on the div and disallow the enter button
$("div").keypress(function(event){
if ( event.which == 13 ) {
event.preventDefault();
}
});
http://jsfiddle.net/pyeyY/
Here you go:
div.onkeydown = function (e) {
if ( e.keyCode === 13 ) { e.preventDefault(); }
};
Live demo: http://jsfiddle.net/3QbCk/
精彩评论