在HTML中,我重复了一系列标记层次结构:
<figure>
<div class="proj-photo">
<img>
<div></div>
<a></a>
</div>
<figcaption></figcaption>
</figure>
当我在非触摸屏上时,我有一个悬停效果,显示
一
标记为启动链接的按钮。
在触摸屏上,我想在整个图中添加一个eventListener,该图将启动其嵌入的
一
按下时标记。
这是我写的,但它不起作用:
const isTouchScreen = window.matchMedia( "(hover: none)" )
if (isTouchScreen.matches){
console.log("Touch screen in use")
const figures = document.querySelectorAll("figure")
const projLinks = document.querySelectorAll("figure > div > a")
var i = 0
function clickHandler(i){
window.open(projLinks[i], '_blank')
}
function assignListeners(i){
figures[i].addEventListener('click', clickHandler(i))
}
for (i in figures) {
assignListeners(i)
}
}