The idea of the program: Check the links , and just on hovering over the link , open the link if it has and textContent "Elzero";
The problem is that the code isn't running as expected and it doesn't open the link on hovering over it (The second link in the list is the one targeted).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<script>
for (let i = 0; i < document.links.length; i ) {
if (document.links[i].classList.contains("open") &&
document.links[i].textContent === "Elzero") {
document.links[i].onmouseenter = function () {
document.links[i].click();
}
}
}
</script>
</head>
<body>
<a href="https://google.com" target="_blank">Google</a>
<a href="https://elzero.org" target="_blank">Elzero</a>
<a href="https://facebook.com" target="_blank">Facebook</a>
<a href="https://linkedin.com" target="_blank">Elzero</a>
</body>
</html>
CodePudding user response:
Place the <script>
right before the closing body tag so that the anchor elements actually exist at the time it is run.
<script>
for (let i = 0; i < document.links.length; i ) {
if (document.links[i].classList.contains("open") &&
document.links[i].textContent === "Elzero") {
document.links[i].onmouseenter = function () {
document.links[i].click();
}
}
}
</script>
</body>