jquery contenteditable linebreak

2020-04-21 03:25发布

I have a content editable area and I'm trying to disable enter/return and shift enter from creating a new paragraph, I have this working with the script below but it disables the buttons all together, what I wish to do is have return just place a line break rather then go to a new paragraph.

$("#content").keypress(function(e) {
    return e.which != 13;
});

All help appreciated!

Cheers,

Wazza

2条回答
SAY GOODBYE
2楼-- · 2020-04-21 03:44

Try something like this:

        $("#content").keypress(function(e) {
            if (e.which == 13) {
                e.preventDefault(); // I think this is the keyword you look for?
                $("#content").val($("#content").val() + "<br/>"); // Handler for new p or line break etc.
            }
        });
查看更多
地球回转人心会变
3楼-- · 2020-04-21 03:51

See this answer on how to place the caret after the inserted node, plus on how to add a node : Set caret position right after the inserted element in a contentEditable div

查看更多
登录 后发表回答