拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何使用Object.assign设定“value”和“for”属性

如何使用Object.assign设定“value”和“for”属性

白鹭 - 2022-03-08 1983 0 0

如何使用 设定元素的值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 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *