如何在老式的javascript div中添加换行符



我必须以某种方式更改以下代码,以在AddRemoteButtonText生成的按钮上的标签上添加换行符。尝试了我在这个网站上找到的所有内容,但没有任何效果(很可能是因为我刚刚开始编写 javascript(。主要是尝试向函数添加另一个变量,然后通过""实现它。

    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,以便我们查看它。

最新更新