gpt4 book ai didi

javascript - 带有 id 的 div 不接受任何事件处理程序 JavaScript。没有Jquery

转载 作者:可可西里 更新时间:2023-11-01 13:11:31 27 4
gpt4 key购买 nike

我试图在您将鼠标悬停在 div 上时暂停定时幻灯片

    <div id="play_slide" onMouseOver="clearTimeout(playTime)"></div>

如果我将 onMouseOver="clearTimeout(playTime)" 放在页面上的 li 中,它会暂停,所以我知道我的代码是正确的,它不会在 div 上工作!此外,如果我去掉 id,当我将警报函数放入事件处理程序时它会发出警报

这是js。

    var playTime;

function playSlide()
{
var slideshow = document.getElementById("play_slide").style;
var images = new Array("an", "complete", "red", "thirteen");
indexPlay++;
if(indexPlay > images.length - 1)
{
indexPlay = 0;
}
slideshow.backgroundImage = "url('assets/images/play/"+images[indexPlay]+".png')";

playTime = setTimeout("playSlide()", 2500);
}

你可以在这里看到这个:www.nicktaylordesigns.com/work.html

最佳答案

我会这样做:

(并且没有内联脚本...只是 <div id="play_slide">Something</div> )

var playTime;
var indexPlay = 0;
var slideElement;
window.onload = function () {
slideElement = document.getElementById("play_slide");
slideElement.addEventListener('mouseenter', function () {
console.log('stop');
clearTimeout(playTime);
});
slideElement.addEventListener('mouseleave', function () {
console.log('continue');
playTime = setTimeout(playSlide, 2500);
});
playSlide();
}

function playSlide() {
var slideshow = slideElement.style;
var images = new Array("an", "complete", "red", "thirteen");
indexPlay++;
if (indexPlay > images.length - 1) {
indexPlay = 0;
}
slideshow.backgroundImage = "url('assets/images/play/" + images[indexPlay] + ".png')";
playTime = setTimeout(playSlide, 2500);
}

Fiddle

关于javascript - 带有 id 的 div 不接受任何事件处理程序 JavaScript。没有Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com