【发布时间】:2021-06-20 15:22:24
【问题描述】:
HTML 代码:
<p><div>
<a href="https://www.digi.com.my/chat-with-us">
<img src="">
</a></p><div>
<p><a href="https://unifi.com.my/support/contact-us"><img src="">
</a></p>
</div>
<p><a>Hey Man
</a>
</p>
<div>
<p><a href="https://unifi.com.my/support/contact-us">Hey man
</a>
</p></div>
Javascript 代码:
let tar = document.querySelectorAll("a[href] > img[src]");
tar.forEach(tarG => {
tarG.addEventListener("mouseover", e => {
const ahref = e.target.a[href];
if (a[href]) location.assign(a[href]);
});
});
首先,我想将 onmouseover() 函数附加到 HTML 页面中所有带有子标记的锚标记中的所有 href 链接。
我的意图是 onmouseover() 函数只激活 href,当鼠标光标移动到包含 img 子标签的锚标签上时。否则,如果没有img子标签,onmouseover()函数不会激活href。
我的代码哪里出错了?感谢您提供所有有用的答案
【问题讨论】:
标签: anchor href addeventlistener onmouseover queryselector