Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在每次按下按钮时打开GIF_Javascript_Html - Fatal编程技术网

Javascript 如何在每次按下按钮时打开GIF

Javascript 如何在每次按下按钮时打开GIF,javascript,html,Javascript,Html,所以一切都正常(除了“if”“else”语句),每次我按下按钮,它都会返回或输出“emotion”(高兴、悲伤、氛围检查失败等)。 但我也希望,对于返回的每一个“情感”,我希望它也能播放特定的GIF。 不要评判哈哈,只是一个小项目(因为我对编码还不熟悉)。顺便说一下,这也是一款应用程序 var形容词=[“快乐”、“悲伤”、“氛围检查失败”、“是”、“有趣”、“愚蠢”、“嘟嘟嘟嘟”、“蒂克托克”、“神奇”、“性感”] var列表; 函数生成器(){ document.getElementById

所以一切都正常(除了“if”“else”语句),每次我按下按钮,它都会返回或输出“emotion”(高兴、悲伤、氛围检查失败等)。 但我也希望,对于返回的每一个“情感”,我希望它也能播放特定的GIF。 不要评判哈哈,只是一个小项目(因为我对编码还不熟悉)。顺便说一下,这也是一款应用程序

var形容词=[“快乐”、“悲伤”、“氛围检查失败”、“是”、“有趣”、“愚蠢”、“嘟嘟嘟嘟”、“蒂克托克”、“神奇”、“性感”]
var列表;
函数生成器(){
document.getElementById(“name”).innerHTML=形容词[Math.floor(Math.random()*形容词.length)]+“”
}
//gif per vibe
if(document.getElementsByClassName(“GetVibe按钮”).innerHTML==形容词[0]){
让url1=”https://media.giphy.com/media/Lb4IZLmCfALhm/giphy.gif"
url1.play()
}else if(形容词===形容词[1]){
让url2=”https://media.giphy.com/media/3o7aCV2Be0Cfk4H8nC/giphy.gif"
console.log(url2)
}else if(形容词===形容词[2]){
让url3=”https://i.imgur.com/fBQFAPm.gif"
console.log(url3)
}else if(形容词===形容词[3]){
让url4=”https://media.giphy.com/media/WJjLyXCVvro2I/giphy.gif"
console.log(url4)
}else if(形容词===形容词[4]){
让url5=”https://media.giphy.com/media/SggILpMXO7Xt6/giphy.gif"
console.log(url5)
}else if(形容词===形容词[5]){
让url6=”https://media.giphy.com/media/24FMDDRwtF4uaMTmEQ/giphy.gif"
console.log(url6)
}else if(形容词===形容词[6]){
让url7=”https://media.giphy.com/media/5MxvgLxp5p732/giphy.gif"
console.log(url7)
}else if(形容词===形容词[7]){
让url8=”https://media.giphy.com/media/WQrVVyPi3qxVSqqyLi/giphy.gif"
console.log(url8)
}else if(形容词===形容词[8]){
让url9=”https://media.giphy.com/media/zz00w5nNFMnPq/giphy.gif"
console.log(url9)
}else if(形容词===形容词[9]){
让url10=”https://media.giphy.com/media/wCSGHsoNgtDa0/giphy.gif"
console.log(url10)
}

.振动检查标题{
背景色:rgb(98211255);
字体大小:20px;
字体系列:Comfortaa;
填充:10雷姆;
}
.获取vibe按钮{
字体系列:Comfortaa;
填充物:75雷姆2.5雷姆;
字号:600;
背景#4242;
z指数:10;
颜色:#fff;
边界半径:30px;
边界:0;
字体大小:14px;
顶部:5雷姆;
左:5雷姆;
保证金:自动;
宽度:50%;
}
#gif砂矿{
利润率:20px;
填充:20px;
}
.说明{
字体大小:15px;
字体系列:Comfortaa;
}
震动检查
说明:
只要按一下按钮。
气氛是

为使其正常工作,进行了许多更改-但现在您可以开始了

var形容词=[“快乐”、“悲伤”、“氛围检查失败”、“是”、“有趣”、“愚蠢”、“嘟嘟嘟嘟”、“蒂克托克”、“神奇”、“性感”];
var gifs=[
"https://media.giphy.com/media/Lb4IZLmCfALhm/giphy.gif",
"https://media.giphy.com/media/3o7aCV2Be0Cfk4H8nC/giphy.gif",
"https://i.imgur.com/fBQFAPm.gif",
"https://media.giphy.com/media/WJjLyXCVvro2I/giphy.gif",
"https://media.giphy.com/media/SggILpMXO7Xt6/giphy.gif",
"https://media.giphy.com/media/24FMDDRwtF4uaMTmEQ/giphy.gif",
"https://media.giphy.com/media/5MxvgLxp5p732/giphy.gif",
"https://media.giphy.com/media/WQrVVyPi3qxVSqqyLi/giphy.gif",
"https://media.giphy.com/media/zz00w5nNFMnPq/giphy.gif",
"https://media.giphy.com/media/wCSGHsoNgtDa0/giphy.gif"
];
函数生成器(){
var index=Math.floor(Math.random()*形容词.length);
document.getElementById(“名称”).innerHTML=形容词[索引]+“”;
document.getElementById(“gif”).src=gifs[index];
}
.vibe检查标题{
背景色:rgb(98211255);
字体大小:20px;
字体系列:Comfortaa;
填充:10雷姆;
}
.获取vibe按钮{
字体系列:Comfortaa;
填充物:75雷姆2.5雷姆;
字号:600;
背景#4242;
z指数:10;
颜色:#fff;
边界半径:30px;
边界:0;
字体大小:14px;
顶部:5雷姆;
左:5雷姆;
保证金:自动;
宽度:50%;
}
#gif砂矿{
利润率:20px;
填充:20px;
}
.说明{
字体大小:15px;
字体系列:Comfortaa;
}

震动检查
说明:只需单击按钮。
气氛是

既然形容词是一个数组,而形容词[1]是数组元素之一,那么
if/else if(形容词===形容词[1])
怎么可能为真呢?同样,如果代码不在函数中,您的
if/else if
会阻止它,所以它在页面上运行一次load@JaromandaX那么我应该如何解决这个问题呢?通过对代码进行重大更改-请参阅answer@JaromandaX该死的兄弟,谢谢你!我真的很感谢你花时间重新编写这篇文章:)@Tanku为了增加一些精确度,
.gif
中的文件是图像,它们不是[HTMLMediaElement][1],你不能在这些文件上使用[play()][2]方法,[
console.log()
][3]不会显示你的图像,而是向web控制台输出消息。[1]: [2]: [3]: