gpt4 book ai didi

Javascript:这个和 onClick

转载 作者:行者123 更新时间:2023-11-28 05:26:13 24 4
gpt4 key购买 nike

我用 4 张图片制作了一个简单的幻灯片演示,点击后,它出现在更大的 div 框中。非常 self 解释,但请随时问我任何问题。由于幻灯片只有 4 张图片,我会保留代码不变。但是如果我有 100 多张图片呢?我认为为每个 onclick 事件复制和粘贴我的 javascript 函数不会像另一个解决方案那样干净。我的问题是如何制作 javascript,以便每当我单击图像时,它都会在框中获取该图像并将其放入大容器中,从而使我的代码总体上更简洁。谢谢。

http://jsfiddle.net/jzhang172/p3Lg96p8/

function clickFunction(){
document.getElementById("big").style.backgroundImage="url('http://assets22.pokemon.com/assets/cms2/img/pokedex/full/007.png')";

}

function clickFunction2(){
document.getElementById("big").style.backgroundImage="url('http://cdn.bulbagarden.net/upload/thumb/f/fb/143Snorlax.png/250px-143Snorlax.png')";

}
function clickFunction3(){
document.getElementById("big").style.backgroundImage="url('http://img2.wikia.nocookie.net/__cb20140410200831/pokemon/images/0/02/025Pikachu_Dream.png')";

}
function clickFunction4(){
document.getElementById("big").style.backgroundImage="url('http://pre03.deviantart.net/27f2/th/pre/f/2012/218/0/7/_004_charmander___1st_attempt_sugimori_style_by_white__flame-d59zqwh.png')";

}
#container{
border:solid 2px;
width:1100px;
height:600px;
position:relative;
overflow:hidden;
}

#big{

border:solid 2px;
border-color:blue;
height:350px;
background:gray;
background-repeat:no-repeat;
background-position:center;
}

.slides{
text-align:center;
margin-top:30px;
border: solid 2px red;
height:200px;

}

.thumb{

border:solid 2px green;
width:260px;
height:100%;
margin:0px;
display:inline-block;

}

.thumb img{
width:100%;
height:100%;
}



.thumb:hover{

border:solid 4px red;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>MySlide</title>

<body>

<div id="container">
Container
<div id="big">
big
</div>

<div class="slides">

<div class="thumb one" onclick="clickFunction()">
<img src="http://assets22.pokemon.com/assets/cms2/img/pokedex/full/007.png">
</div>
<div class="thumb 2"onclick="clickFunction2()">
<img src="http://cdn.bulbagarden.net/upload/thumb/f/fb/143Snorlax.png/250px-143Snorlax.png">
</div>
<div class="thumb 3"onclick="clickFunction3()">
<img src="http://img2.wikia.nocookie.net/__cb20140410200831/pokemon/images/0/02/025Pikachu_Dream.png">
</div>
<div class="thumb 4"onclick="clickFunction4()">
<img src="http://pre03.deviantart.net/27f2/th/pre/f/2012/218/0/7/_004_charmander___1st_attempt_sugimori_style_by_white__flame-d59zqwh.png">
</div>





</div>


</body>

最佳答案

这是使用 url 数组的更优雅的方法:

var urls = [
"http://assets22.pokemon.com/assets/cms2/img/pokedex/full/007.png",
"http://cdn.bulbagarden.net/upload/thumb/f/fb/143Snorlax.png/250px-143Snorlax.png",
"http://img2.wikia.nocookie.net/__cb20140410200831/pokemon/images/0/02/025Pikachu_Dream.png",
"http://pre03.deviantart.net/27f2/th/pre/f/2012/218/0/7/_004_charmander___1st_attempt_sugimori_style_by_white__flame-d59zqwh.png"
];

function clickFunction(num) {
document.getElementById("big").style.backgroundImage = "url('" + urls[num] + "')";
}

然后您可以为您的 div 使用 clickFunction(0)clickFunction(1)clickFunction(2)点击

或者,您也可以将该部分自动化:

var thumbs = document.getElementsByClassName("thumb");
for (var i = 0; i < thumbs.length; i++) {
thumbs[i].onclick = (function(index){
return function() {
document.getElementById("big").style.backgroundImage = "url('" + urls[index] + "')"
}
})(i);
}

关于Javascript:这个和 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171591/

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