简体   繁体   English

如何在 JavaScript 中创建新行?

[英]How do I create a new line in JavaScript?

I have appended a textbox to a div area.我已将一个文本框附加到一个 div 区域。 However I want it to goto a new line in that div after it does that, so my for loop prints a column of textboxes instead of a row.但是,我希望它在执行此操作后转到该 div 中的新行,因此我for循环打印一列文本框而不是一行。

I tried this:我试过这个:

<div id="timearea"> </div>
var br = '<br/>';
br.appendTo("#timearea");

However this does not work.但是,这不起作用。 What would the code be?代码是什么?

You would need to create the element using the createElement() method then append the child to the element using the appendChild() method您需要使用createElement()方法创建元素,然后使用appendChild()方法将子元素 append 创建到元素

var br = document.createElement("br");
document.getElementById("timearea").appendChild(br);

I suggest you apply CSS styling to your divs to control how they are laid out.我建议您将 CSS 样式应用于您的 div 以控制它们的布局方式。 You can add attributes to style inline or (preferably) add classes to assign styles via JavaScript.您可以将属性添加到样式内联或(最好)添加类以通过 JavaScript 分配 styles。

The display attribute controls the flow - display:block should do it. display属性控制流程 - display:block应该这样做。

.my_block_class {
  display:block;
}

You can then add the class with JavaScript like so :然后,您可以像这样添加 class 和 JavaScript :

document.getElementById("timearea").className += "my_block_class";

Or, if you want to do it without classes:或者,如果你想在没有课程的情况下这样做:

document.getElementById("timearea").style.display = "block";

Not sure if you mean textarea or input type="text" but regardless, it is better to do this in CSS.不确定您的意思是textarea还是input type="text"但无论如何,最好在 CSS 中执行此操作。 In your CSS file or inline style description add:在您的 CSS 文件或内联样式描述中添加:

#timearea input {
  display:block;
}

If it's an input element you are adding, or:如果它是您要添加的input元素,或者:

#timearea textarea {
  display:block;
}

If it's a textarea .如果是textarea

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

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