gpt4 book ai didi

Javascript - 修改脚本以将换行符转换为

转载 作者:行者123 更新时间:2023-11-28 02:11:49 30 4
gpt4 key购买 nike

我对 JavaScript 很糟糕。我正在使用以下代码,我希望在文本区域中输入的换行符变为 <br>标签。我已经环顾了一段时间,似乎找不到任何有用的东西,所以如果有人能帮助我那就太好了!

    ////////////////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){

}
}
}

最佳答案

尝试修改代码中的以下行,并注意我添加的其他“.replace”调用。他们将新行替换为

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

编辑:我把替换放在了错误的地方。我相信这个更新的代码应该仅将来自文本区域的文本中的换行符替换为
。以前,我是在整个 HTML 上执行此操作。

关于Javascript - 修改脚本以将换行符转换为 <br>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16975970/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com