简体   繁体   中英

Javascript - modifying script to convert line breaks to <br>

I'm horrible with javascript. I'm using the following code and I want line breaks entered into the text area to become <br> tags. I've been looking around for a while and can't seem to find anything that works so if anyone could help me out that'd be great!

    ////////////////SETTINGS//////////////
var editableElements = new Array("p", "h1","ul"); //List of editable elements
var doubleClick = false; //If false, single click is used to start editing
var allowNull = true; //If a editable region is allowed to be blank
var nullValue = "none"; //If it is blank, what should it show
var textEditorSize = 50; //In px
//////////////////////////////////////
var currentlyEditing = false;
function loadEditor(actionFile){
  contents = document.body.innerHTML;
  document.body.innerHTML = '<form action="' + actionFile + '" method="post" id="editor429_form"><input type="button" value="Save Page" name="savepage" onclick="savePage(this)" /><input type="hidden" name="pagecontents" value="" /></form>' + contents;
  document.onkeydown = function(event){ changeEditor(event); }
  for(z = 0; z <= editableElements.length - 1; z++){
    elements = document.getElementsByTagName(editableElements[z]);
    for(i = 0; i <= elements.length - 1; i++){
      if(doubleClick == true){
        elements[i].ondblclick = function(){ startEditor(this); }
      }else{
        elements[i].onclick = function(){ startEditor(this); }
      }
    }
  }
}
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}


function startEditor(me){
  if(currentlyEditing == false){
    contents = me.innerHTML;
    me.innerHTML = '<textarea class="editor429_editor" id="editorBox" style="width: 100%;height:' + textEditorSize + 'px;" onblur="endEditor(this)">' + contents + '</textarea>';
    currentlyEditing = true;
    document.getElementById('editorBox').focus();
  }
}
function endEditor(me){
  contents = me.value;
  if(contents.trim() == "") contents = nullValue;
  me.parentNode.innerHTML = contents;
  currentlyEditing = false;
}
function savePage(me){
  me.parentNode.pagecontents.value = (document.body.innerHTML.search('</form>') != -1) ? document.body.innerHTML.trim().substr(document.body.innerHTML.indexOf('</form>') + 7):document.body.innerHTML.trim().substr(document.body.innerHTML.indexOf('</FORM>') + 7);
  me.parentNode.submit();
}
function changeEditor(e){
  //38 up
  //40 down
  if((e.keyCode == 38 || e.keyCode == 40 ) && e.altKey == true && currentlyEditing == true){
    if(e.keyCode == 38){
      document.getElementById('editorBox').style.height = "50px";
    }else if(e.keyCode == 40){

    }
  }
}

Try modifying the following line in your code, and note the additional '.replace' calls I added. They replace the new lines with

   function endEditor(me){
      contents = me.value.replace(/\n/g, '<br />');
      if(contents.trim() == "") contents = nullValue;
      me.parentNode.innerHTML = contents;
      currentlyEditing = false;
   }

Edit: I had put the replace in the wrong place. I believe this updated code should replace the line breaks with
from the text coming from the textarea only. Previously, I was doing that on the entire HTML.

try replacing all the '\\r', '\\n', '\\r\\n' and '\\n\\r' with

<br>

I would do a replace on you content var.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM