如果我在 HTML 文件中放入一些脚本来改变动画播放状态,它是有效的。但是当我将脚本放入脚本文件时,它不起作用。谁能告诉我为什么? 像这样的脚本:
var buttons=document.getElementsByTagName("button");
for(var i=0;i<buttons.length;i++) {
console.log(buttons.length);
buttons[i].onclick=function(e) {
document.getElementById("para")
.style.WebkitAnimationPlayState=e.target.innerHTML;
};
}
最佳答案
元素可能未在文档
中定义。尝试将现有脚本放在 window
load
事件处理程序中
function toggleAnimationPlayState() {
var buttons=document.getElementsByTagName("button");
for(var i=0;i<buttons.length;i++) {
console.log(buttons.length);
buttons[i].onclick=function(e) {
document.getElementById("para")
.style.WebkitAnimationPlayState=e.target.innerHTML;
};
}
}
window.addEventListener("load", toggleAnimationPlayState);
关于javascript - 改变动画播放状态的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39689066/