gpt4 book ai didi

javascript - 如何使用javascript插入换行符?

转载 作者:太空狗 更新时间:2023-10-29 13:33:00 28 4
gpt4 key购买 nike

我的提交表单包含姓名、电子邮件和电话号码字段。当然,我想在三者之间插入换行符。我试图在所有三个之间插入一个换行符,但只在表单末尾生成一个换行符,而我的代码应该附加 3 个换行符,而不是一个。

生成的 View 源显示如下:

<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>

生成此表单的 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);
}

最佳答案

您应该创建新的 <br>每次附加它时标记,例如

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

DEMO

关于javascript - 如何使用javascript插入换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22272623/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com