Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从“创建输入标签”;至于;属性,而不更改HTML_Javascript_Html_For Loop_Dom - Fatal编程技术网

Javascript 从“创建输入标签”;至于;属性,而不更改HTML

Javascript 从“创建输入标签”;至于;属性,而不更改HTML,javascript,html,for-loop,dom,Javascript,Html,For Loop,Dom,在大学课程中学习JavaScript时遇到问题。这是我的最后一个项目,HTML代码的问题是为一个没有id的标签创建一个输入字段,但它有一个“for”元素。不能使用JQuery或JSON创建它 因此HTML看起来像这样: 这不是设置类型和id的方式。试试这个 var firstCar = document.createElement("input"); firstCar.type = "text"; // set the type firstCar.id = "car"; // set

在大学课程中学习JavaScript时遇到问题。这是我的最后一个项目,HTML代码的问题是为一个没有id的标签创建一个输入字段,但它有一个“for”元素。不能使用JQuery或JSON创建它

因此HTML看起来像这样:
这不是设置
类型
id
的方式。试试这个

var firstCar = document.createElement("input");

firstCar.type = "text"; // set the type

firstCar.id = "car";    // set the ID

var position = document.getElementsByTagName("label")[1]; // it should be getElements... not getElement...

position.appendChild(firstCar);

仅供参考-当
输入
元素未指定
类型
属性时,它默认为
。我们的网页已更改,这不再是要求。然而,如果有人想回答这个问题,我还是希望能了解更多关于DOM操作的知识。谢谢你,马库斯,这是getElementsByTagName,注意复数。
var firstCar = document.createElement("input");

firstCar.type = "text"; // set the type

firstCar.id = "car";    // set the ID

var position = document.getElementsByTagName("label")[1]; // it should be getElements... not getElement...

position.appendChild(firstCar);