簡體   English   中英

如何在oldschool javascript div中添加換行符

[英]How to add a line break in oldschool javascript div

我必須以某種方式更改以下代碼,以將換行符添加到由AddRemoteButtonText生成的按鈕上的標簽上。 嘗試了我在此站點上找到的所有內容,但沒有任何效果(很可能是因為我剛剛開始編寫JavaScript)。 通常嘗試在功能中添加另一個變量,然后通過“ \\ n”實現它。

    var size = 20;
var repeat_timer;

function AddRemoteButtonText(num, posx, posy, wx, wy, color, label, remote, command, initial_delay, repeat_delay) 
{
    document.write('<div id="' + num + '" class="button ' + color + '" style="position:absolute; top:' + posy + 'px; left:' + posx + 'px; height:' + wy + 'px; width:' + wx + 'px; line-height: ' + wy + 'px;" align="center">' + label + '</div>');
    document.getElementById(num).onmouseup = function () { SendIRCommand (num,remote,command,initial_delay,repeat_delay); EndSendCommand (num) };
    document.getElementById(num).onmouseleave = function () { EndSendCommand (num) };
}

    }
};

感謝您的回答!

像下面的示例一樣添加'<br>'

document.write(variable +'<br/>'+ variable2);

我可能會這樣做(出於示例目的而簡化):

 var i = 0; var aboveLineBreak; var belowLineBreak; function AddRemoteButtonText() { console.log(i); //define some text to print to the button aboveLineBreak = "above br" + i; belowLineBreak = "below br" + i; //create a button object var btn = document.createElement("DIV"); //add our text, including the line break, to the button btn.appendChild(document.createTextNode(aboveLineBreak)); btn.appendChild(document.createElement("BR")); btn.appendChild(document.createTextNode(belowLineBreak)); //define our button's CSS style btn.setAttribute("class", "button red"); //to set the position, do: //btn.style.position = "absolute"; //btn.style.top = whatever; //and so on... //add listeners if needed... btn.onmouseup = function () { console.log("foo"); }; //finally add the button to the document's body and increment i document.body.appendChild(btn); i++; } 
 .button { width: 33%; height: 45px; } .red { background-color: darkred; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <button onClick="AddRemoteButtonText();"> Add Remote Button Text </button> </body> </html> 

不是最漂亮的按鈕,但是如果您用自己的CSS替換我的CSS,則應該有一個有效的換行符。 如果沒有,我們將需要您發布CSS,以便我們進行查看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM