How to parse editable DIV's text with browser compatibility

后端 未结 3 470
孤街浪徒
孤街浪徒 2021-01-14 07:33

I make div as editable. while i tried to parse div\'s text, i was needed to do the below regular expression.

innerDOM = \"
3条回答
  •  没有蜡笔的小新
    2021-01-14 07:55

    i found this solution in this site:

    $editables = $('[contenteditable=true'];
    
    $editables.filter("p,span").on('keypress',function(e){
     if(e.keyCode==13){ //enter && shift
    
      e.preventDefault(); //Prevent default browser behavior
      if (window.getSelection) {
          var selection = window.getSelection(),
              range = selection.getRangeAt(0),
              br = document.createElement("br"),
              textNode = document.createTextNode($("
     
    ").text()); //Passing " " directly will not end up being shown correctly range.deleteContents();//required or not? range.insertNode(br); range.collapse(false); range.insertNode(textNode); range.selectNodeContents(textNode); selection.removeAllRanges(); selection.addRange(range); return false; } } });

提交回复
热议问题