简体   繁体   中英

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.

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")
Second way of setting attribute:

 let creatingElement = `<tabel id="value"></tabel>`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM