Warm tip: This article is reproduced from stackoverflow.com, please click
javascript

How to add anchor tags dynamically to a div in Javascript?

发布于 2020-03-27 10:23:01

How to add a list of hyperlinks (with their events and properties) dynamically to a div in Javascript?

Questioner
Ahmad Farid
Viewed
15
321 2019-08-22 03:38

here's a pure Javascript alternative:

var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href',"yourlink.htm");
aTag.innerText = "link text";
mydiv.appendChild(aTag);