具有ID的div将不接受任何事件处理程序JavaScript.没有jQuery
作者:互联网
我正在将鼠标悬停在div上时试图暂停定时幻灯片播放
<div id="play_slide" onm ouseOver="clearTimeout(playTime)"></div>
如果我在页面上的li内放onMouseOver =“ clearTimeout(playTime)”,它将暂停,所以我知道我的代码是正确的,它将无法在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”>某事< / 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);
}
标签:slideshow,event-handling,css,html,javascript 来源: https://codeday.me/bug/20191122/2059455.html