简体   繁体   English

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

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

I have to alter the following code somehow to add line breaks to the labels on the buttons generated by AddRemoteButtonText. 我必须以某种方式更改以下代码,以将换行符添加到由AddRemoteButtonText生成的按钮上的标签上。 Tried everything I found on this site, but nothing worked yet (most probably because I`m just starting out coding javascript). 尝试了我在此站点上找到的所有内容,但没有任何效果(很可能是因为我刚刚开始编写JavaScript)。 Mostly tried adding another variable to the function and then implementing it by "\\n" . 通常尝试在功能中添加另一个变量,然后通过“ \\ 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) };
}

    }
};

Thanks for your answer! 感谢您的回答!

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

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

I would probably do it like this (simplified for example purposes): 我可能会这样做(出于示例目的而简化):

 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> 

Not the most beautiful buttons, but if you simply substitute my CSS with your own you should have a working line break. 不是最漂亮的按钮,但是如果您用自己的CSS替换我的CSS,则应该有一个有效的换行符。 If not, we will need you to post your CSS so we can have a look at it. 如果没有,我们将需要您发布CSS,以便我们进行查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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