avoid ie contentEditable element to create paragraphs on Enter key

后端 未结 5 1879
小蘑菇
小蘑菇 2020-12-06 06:49

On InternetExplorer, a contentEditable DIV creates a new paragraph (

) each time you press Enter whereas Firefox creates a
5条回答
  •  醉梦人生
    2020-12-06 07:28

    Here's a solution (uses jQuery). After you click on the 'Change to BR' button, the
    tag will be inserted instead of the

    tag.

    Html:

    This is a division that is content editable. You can position the cursor within the text, move the cursor with the arrow keys, and use the keyboard to enter or delete text at the cursor position.

    Javascript:

    function InsertBR()
    {
        $("#editable").keypress(function(e) {
            if (e.which == 13) 
            {
                e.preventDefault();
                document.selection.createRange().pasteHTML("
    ") } }); } function ViewSource() { var div = document.getElementById('editable'); alert('div.innerHTML = ' + div.innerHTML); }

    These links helped. Working example here.

提交回复
热议问题