Contenteditable paragraph tag on enter

后端 未结 3 1015
迷失自我
迷失自我 2020-12-30 05:01

I was wondering if there is an acceptable way to force all major browsers to insert paragraph tag instead of the default tag that they insert on pressing enter key when cont

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 05:31

    I had this same problem and found the solution (CHROME, MSIE, FIREFOX), follow my code in the link.

    $(document).on('click','#myButton',function() {
        if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
            var str = $('#myDiv').html().replace(/
    /gi,'').replace(/
    /gi,'
    ').replace(/<\/div>/gi,''); else if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) var str = $('#myDiv').html().replace(/<\/br>/gi,'').replace(/
    /gi,'
    ').replace(/<\/br>/gi,''); else if (navigator.userAgent.toLowerCase().indexOf("msie") == -1) var str = $('#myDiv').html().replace(/
    /gi,'').replace(/

    /gi,'
    ').replace(/<\/p>/gi,''); $('#myDiv2').removeClass('invisible').addClass('visible').text(str); $('#myDiv3').removeClass('invisible').addClass('visible').html(str); });

    https://jsfiddle.net/kzkxo70L/1/

提交回复
热议问题