如何使用 设定元素的值Object.assign()?"value"除了a<input>和"for"a之外,所有其他属性似乎都出现了<label>。这是代码:
window.onload = function createForm () {
    // Form header >>>>
    const formHeaderText = document.createTextNode("Form");
    headerEl.appendChild(formHeaderText);
    container.appendChild(headerEl);
    // Form element >>>
    Object.assign(formEl, {
        name: "inputForm",
        id: "myForm"
    });
   container.appendChild(formEl);
    // Name Label >>>
    const nameLabelText = document.createTextNode("Name");
    labelElName.appendChild(nameLabelText);
    Object.assign(labelElName, {
        for: "iname",
    });
    formEl.appendChild(labelElName);
    // Name Input element >>>
    formEl.appendChild(inputElName);
    Object.assign(inputElName, {
        className: "inputs",
        id: "iname",
        name: "name",
        type: "text",
        placeholder: "Name",
        value: ""
    })
我得到的是:
<input class="inputs" id="iname" name="name" type="text" placeholder="Name">
对于输入和:
<label>Name</label>
对于标签元素。
uj5u.com热心网友回复:
由于for是 JavaScript 中的保留关键字,因此您需要在元素上使用htmlFor属性,如下所示:label
Object.assign(labelElName, {htmlFor: "iname"});
或者
labelElName.htmlFor = "iname";

 
							 
										
										 
										
										 
										
										
										 
										
										 
										
										 
										
										
0 评论