I want all links in a certain div (which has a class) to open in a new window.
How can this be done using Javascript?
I'm just learning the basics of Javascript and I came across the following suggestion (from another post on this site), but it does not target all links under the specified class, which is where I'm stuck:
document.getElementsByClassName('vertical-tabs-active-tab')[0].setAttribute("value", "yolo");
Try this for div having class as bold :)
var links = document.querySelectorAll("div.bold a");
links.forEach(link => {
link.setAttribute('target', '_blank');
})
Can't comment on @UtkarshPramodGupta post. It's his proposed solution but you need querySelectorAll instead of querySelector:
var links = document.querySelectorAll("div.bold a");
links.forEach(link => {
link.setAttribute('target', '_blank');
})
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.