【问题标题】:Javascript: How do I create element with id class and id which is some variable?Javascript:如何创建具有 id 类和 id 的元素,这是一些变量?
【发布时间】:2022-02-01 03:32:14
【问题描述】:

Javascript:如何创建具有 id 类和 id 的元素,这是一些变量? 我没有给出 ID 作为某个值,但 ID 等于某个变量

【问题讨论】:

标签: javascript html dom


【解决方案1】:

let id="theID", cls="MyClass"; // these are the "variables" ...

const div = document.createElement("div");
div.id=id; div.className=cls;div.textContent="This is the new div."
console.log(div.outerHTML);  // preview of the created markup
document.body.append(div)

【讨论】:

    【解决方案2】:

    检查 DOM Manipulation of JavaScript 方法:

    document.createElement('tag or div');

    document.querySelector('id or class');

    document.querySelector('id or class').appendChild(htmlElement);

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2015-04-27
      • 2011-06-08
      • 1970-01-01
      • 2013-05-07
      相关资源
      最近更新 更多