简体   繁体   English

如何用javascript插入换行符?

[英]How to insert a line break with javascript?

The Submit Form I have has the name, email, and phone number fields.我拥有的提交表单包含姓名、电子邮件和电话号码字段。 Of course, I want to insert line break between the three.当然,我想在三者之间插入换行符。 I tried to insert a line break between all three, but only generated one at the end of the form, when my code should appended 3 line breaks, not one.我试图在所有三个换行符之间插入一个换行符,但只在表单末尾生成了一个换行符,当时我的代码应该附加 3 个换行符,而不是一个。

The generated view source shows this:生成的视图源显示:

<label>Name: </label><input required="" name="fullName" type="text"><label>Email: </label>   <input required="" name="email" type="text"><label>Phone Number: </label><input required="" name="phoneNumber" type="text"><br><input value="Submit Query" type="submit"></form>

My Javascript Code to produce this form:我的 Javascript 代码生成此表单:

function queryForm()
{
    var queryBox = document.getElementById("queryBox").style.display = "block";
    var queryForm = document.getElementById("queryForm");
    var linebreak = document.createElement("br");

    var lblName = document.createElement("label");
    lblName.textContent = "Name: ";
    queryForm.appendChild(lblName);

    var fullName = document.createElement("input");
    fullName.name = "fullName";
    fullName.type = "text";
    fullName.required = "required";
    queryForm.appendChild(fullName);
    queryForm.appendChild(linebreak);


    var lblEmail = document.createElement("label");
    lblEmail.textContent = "Email: ";
    queryForm.appendChild(linebreak);
    queryForm.appendChild(lblEmail);

    var email = document.createElement("input");
    email.name = "email";
    email.type = "text";
    email.required = "required";
    queryForm.appendChild(email);


    var lblPhoneNumber = document.createElement("label");
    lblPhoneNumber.textContent = "Phone Number: ";
    queryForm.appendChild(linebreak);
    queryForm.appendChild(lblPhoneNumber);

    var phoneNumber = document.createElement("input");
    phoneNumber.name = "phoneNumber";
    phoneNumber.type = "text";
    phoneNumber.required = "required";
    queryForm.appendChild(phoneNumber);


    var submitQuery = document.createElement("input");
    submitQuery.type = "submit";
    submitQuery.value = "Submit Query";
    queryForm.appendChild(linebreak);
    queryForm.appendChild(submitQuery);
}

You should create new <br> tag each time when you will append it, something like每次添加时都应该创建新的<br>标签,例如

linebreak = document.createElement("br");
queryForm.appendChild(linebreak);

DEMO演示

The problem with your code is you're inserting the same element over and over again.您的代码的问题是您一遍又一遍地插入相同的元素。

Here's an analogy: Imagine you have several children lined up in a row.这是一个类比:假设您有几个孩子排成一排。 Yes you can attempt to move one of them after each of the other children, however, at the end of the day you still only have one of that child.是的,您可以尝试在每个其他孩子之后移动其中一个,但是,在一天结束时,您仍然只有一个孩子。 He's going to end up at the end of the row.他将排在最后。

Solution: Create the line break element each time you need to insert it.解决方案:每次需要插入时创建换行元素。

Also, here's an obligatory plug for jQuery: http://www.jquery.com此外,这是 jQuery 的强制性插件: http : //www.jquery.com

You should try to append each time a new node, and not the same one that was previously created, ie:您应该尝试每次添加一个新节点,而不是之前创建的同一个节点,即:

queryForm.appendChild(document.createElement("br"));

EDIT: the explanation is here on the documentation编辑:解释在文档中

Node.appendChild Adds a node to the end of the list of children of a specified parent node. Node.appendChild将一个节点添加到指定父节点的子节点列表的末尾。 If the node already exists it is removed from current parent node, then added to new parent node.如果该节点已存在,则将其从当前父节点中删除,然后添加到新的父节点中。

https://developer.mozilla.org/en-US/docs/Web/API/Node.appendChild https://developer.mozilla.org/en-US/docs/Web/API/Node.appendChild

Protip: Append your input s to your label s.提示:将您的input s 附加到您的label s 中。 That way a user can click the label to get focus on the input.这样,用户可以单击标签以专注于输入。

This has the additional perk that you can simply apply the CSS display:block to your label s and have them on separate lines!这有额外的好处,您可以简单地将 CSS display:block到您的label并将它们放在单独的行上!

You can use the classic \\n to get this to work.您可以使用经典的 \\n 来使其工作。 I used this solution for a multi-line textarea in my project, but I cut out some code to simplify the example:我在我的项目中将这个解决方案用于多行文本区域,但我删减了一些代码以简化示例:

explanation.value = 'Equation: ' + equation;

explanation.value += '\nAnswer: ' + answer;

在此处输入图片说明

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

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