[英]Jquery: Show/Hide Div broken
Scenario: I'm trying to create a script that allows a user to click a thumbnail image and display a larger image in preview box. 场景:我正在尝试创建一个脚本,该脚本允许用户单击缩略图并在预览框中显示较大的图像。 I'm using a div and a img for this example just as a placeholder for now.
我现在在这个例子中使用div和img作为占位符。
Question In Jquery, is there a way to use a function with parameters as a callback for a onclick event listener? 问题在Jquery中,是否可以使用带有参数的函数作为onclick事件侦听器的回调? I've looked at
addEventListener
and Onclick
and .on
method in jquery, but nothing seems to fit what i'm searching for. 我已经看过jquery中的
addEventListener
和Onclick
和.on
方法,但是似乎没有什么适合我在搜索。
My Attempts: 我的尝试:
http://jsfiddle.net/davideugenepeterson/9emhuzw0/3/ http://jsfiddle.net/davideugenepeterson/9emhuzw0/3/
http://jsfiddle.net/EhtrR/931/ http://jsfiddle.net/EhtrR/931/
Javascript: Javascript:
var numberOfImages = 4;
var imageArray = [];
//since i'm going to hide all other divs each time a img is clicked,
// I need to be calling from imageArray[] so I can difArray check later
for (i = 0; i <= numberOfImages; i++) {
//counting starts from zero, but frontend already built their naming convention to start with 1
if (i === 0) {
continue
}
imageArray.push(i);
}
for (i = 0; i <= imageArray.length; i++) {
if (i === 0) {
continue
}
//on each img click show corresponding div and hide all others, not working
$("#img-" + imageArray[i]).on('click', function () {
$("#div-" + i).show();
});
}
I don't understand what you're trying to do with an array. 我不明白您要使用数组做什么。 Give the image something relating to a corresponding element you want to be shown (I used data attribute in example).
给图像添加与您要显示的对应元素有关的内容(我在示例中使用了data属性)。 And give a collective class name to all the elements that are going to be shown/hidden.
并为将要显示/隐藏的所有元素指定一个集体类名称。
Then on click, hide all and show the one related to what was clicked. 然后单击,隐藏所有内容并显示与单击内容相关的内容。
$('img').click(function(){
$('.somename').hide();
$('#div-'+$(this).data("number")).show();
});
Another solution (four variations) 另一个解决方案(四种变体)
a) 一种)
// JavaScript
var img = document.getElementsByTagName('IMG'),
div= document.getElementsByTagName('DIV'),
current, i;
for(i = 0; i < img.length; i++){
(function(i){
img[i].onclick = function(){
div[i].style.display = 'block';
if(current) current.style.display = 'none';
current = div[i];
};
})(i);
}
b) b)
var img = document.getElementsByTagName('IMG'),
div= document.getElementsByTagName('DIV'),
current, i;
for(i = 0; i < img.length; i++){
(function(i){
img[i].onclick = function() { callback(i); };
})(i);
}
function callback(i){
div[i].style.display = 'block';
if(current) current.style.display = 'none';
current = div[i];
}
c) C)
var img = document.getElementsByTagName('IMG'),
div= document.getElementsByTagName('DIV'),
current, i;
for(i = 0; i < img.length; i++){
img[i].onclick = callback.call(this,i);
}
function callback(i){
return function () {
div[i].style.display = 'block';
if(current) current.style.display = 'none';
current = div[i];
};
}
<!-- HTML -->
<style>
div {display:none;}
</style>
<img src="01.jpg" alt="" />
<img src="02.jpg" alt="" />
<img src="03.jpg" alt="" />
<img src="04.jpg" alt="" />
<img src="05.jpg" alt="" />
<div>first</div>
<div>second</div>
<div>third</div>
<div>fourth</div>
<div>fifth</div>
d) jQuery version d)jQuery版本
(function(){
var div = $('div'), current;
$('img').each(function(index){
$(this).bind ('click', function(){
$('div:nth(' + index + ')').show();
if(current) current.hide();
current = $('div:nth(' + index + ')');
});
});
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.