开发者

Restrict carriage return on writable div

开发者 https://www.devze.com 2023-04-03 04:16 出处:网络
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

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消