[英]How do I create an HTML element with attributes and events using JavaScript?
I was working on a school project on discovering JavaScript and am stuck with Element creating, so I would like to know how do I need to implement this.我正在做一个关于发现 JavaScript 的学校项目,并且一直坚持创建元素,所以我想知道我需要如何实现它。
Create element:创建元素:
var element = document.createElement('table');
Adding attribute:添加属性:
element.setAttribute( "id", "table1" );
there are two ways to create element and set attribute.创建元素和设置属性有两种方法。
Creating Element创建元素
first way of creating element:第一种创建元素的方法:
let nameOfElement = document.createElement("tabel")
second way of creating element:第二种创建元素的方式:
let elementName = `<tabel></tabel>`
Now setting attributes现在设置属性
First way of setting attribute:第一种设置属性的方法:
nameOfElement.setAttribute("id", "the-value")
let creatingElement = `<tabel id="value"></tabel>`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.