Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 在事件侦听器中将静态值作为参数发送_Javascript_Jquery_Event Listener - Fatal编程技术网

Javascript 在事件侦听器中将静态值作为参数发送

Javascript 在事件侦听器中将静态值作为参数发送,javascript,jquery,event-listener,Javascript,Jquery,Event Listener,使用Javascript(和jQuery),我有一个由多个对象组成的数组。对于该数组中的每个对象,我将向div添加一个可单击的小图像。单击每个小图像将更改一个大图像的路径,从而显示一个新的图片 以下是我得到的: for (s = 0; s < splash.length; s += 1) { // add a new dot to this row $(".splash_dots").append ("<div class = 'dot' id = 'dot" + s +

使用Javascript(和jQuery),我有一个由多个对象组成的数组。对于该数组中的每个对象,我将向div添加一个可单击的小图像。单击每个小图像将更改一个大图像的路径,从而显示一个新的图片

以下是我得到的:

for (s = 0; s < splash.length; s += 1)
  {
  // add a new dot to this row
  $(".splash_dots").append ("<div class = 'dot' id = 'dot" + s + "'><img src = 'images/dot2.png'></div>");

  var id = 'dot' + s;

  // add a click handler
  document.getElementById (id).addEventListener ("click", function () {change_splash (s)});
  }

//////////////////////////////////////////////////

function change_splash (s)
  {
  // load a new image for the large photo
  $(".splash").attr ("src", splash[s].screenshot_link);
  }
这看起来真的很低效

我基本上只是在尝试创建一个滑动飞溅广告,就像你在几乎任何零售主页上看到的那样,所以我知道这是可能的。欢迎提出任何想法。

您需要结束:

(function(id,s){
 document.getElementById(id)
  .addEventListener("click", function(){change_splash (s)});}(id,s));
您需要关闭:

(function(id,s){
 document.getElementById(id)
  .addEventListener("click", function(){change_splash (s)});}(id,s));
您需要关闭:

(function(id,s){
 document.getElementById(id)
  .addEventListener("click", function(){change_splash (s)});}(id,s));
您需要关闭:

(function(id,s){
 document.getElementById(id)
  .addEventListener("click", function(){change_splash (s)});}(id,s));
试试这个

for (var s in splash){
    // create the points, stores the id in the data-id attribute
    $(".splash_dots").append ("<div class ='dot' data-id='" + s + "'><img src='images/dot2.png'></div>");
}

// click listener
$("body").on("click",".dot",function(){
    // retrieve the id from the data-id attribute
    var s = $(this).attr("data-id");
    $(".splash").attr ("src", splash[s].screenshot_link);
});
for(飞溅中的变量s){
//创建点,将id存储在“数据id”属性中
$(“.splash_dots”)。追加(“”);
}
//单击侦听器
$(“body”)。在(“click”、“.dot”,function()上{
//从数据id属性中检索id
var s=$(this.attr(“数据id”);
$(“.splash”).attr(“src”,splash[s]。屏幕截图_链接);
});
请在中查看它的实际操作,并尝试使用此功能

for (var s in splash){
    // create the points, stores the id in the data-id attribute
    $(".splash_dots").append ("<div class ='dot' data-id='" + s + "'><img src='images/dot2.png'></div>");
}

// click listener
$("body").on("click",".dot",function(){
    // retrieve the id from the data-id attribute
    var s = $(this).attr("data-id");
    $(".splash").attr ("src", splash[s].screenshot_link);
});
for(飞溅中的变量s){
//创建点,将id存储在“数据id”属性中
$(“.splash_dots”)。追加(“”);
}
//单击侦听器
$(“body”)。在(“click”、“.dot”,function()上{
//从数据id属性中检索id
var s=$(this.attr(“数据id”);
$(“.splash”).attr(“src”,splash[s]。屏幕截图_链接);
});
请在中查看它的实际操作,并尝试使用此功能

for (var s in splash){
    // create the points, stores the id in the data-id attribute
    $(".splash_dots").append ("<div class ='dot' data-id='" + s + "'><img src='images/dot2.png'></div>");
}

// click listener
$("body").on("click",".dot",function(){
    // retrieve the id from the data-id attribute
    var s = $(this).attr("data-id");
    $(".splash").attr ("src", splash[s].screenshot_link);
});
for(飞溅中的变量s){
//创建点,将id存储在“数据id”属性中
$(“.splash_dots”)。追加(“”);
}
//单击侦听器
$(“body”)。在(“click”、“.dot”,function()上{
//从数据id属性中检索id
var s=$(this.attr(“数据id”);
$(“.splash”).attr(“src”,splash[s]。屏幕截图_链接);
});
请在中查看它的实际操作,并尝试使用此功能

for (var s in splash){
    // create the points, stores the id in the data-id attribute
    $(".splash_dots").append ("<div class ='dot' data-id='" + s + "'><img src='images/dot2.png'></div>");
}

// click listener
$("body").on("click",".dot",function(){
    // retrieve the id from the data-id attribute
    var s = $(this).attr("data-id");
    $(".splash").attr ("src", splash[s].screenshot_link);
});
for(飞溅中的变量s){
//创建点,将id存储在“数据id”属性中
$(“.splash_dots”)。追加(“”);
}
//单击侦听器
$(“body”)。在(“click”、“.dot”,function()上{
//从数据id属性中检索id
var s=$(this.attr(“数据id”);
$(“.splash”).attr(“src”,splash[s]。屏幕截图_链接);
});

中查看它的实际操作,或者您可以像@Tom一样使用循环体作为闭包@Markasoftware:他的建议既不会创建闭包,也不会产生预期的结果。这非常有效。我在闭包中移动了“var id”,以消除该参数并稍微简化一些事情。或者你可以像@Tom一样使用循环体作为闭包@Markasoftware:他的建议既不会创建闭包,也不会产生预期的结果。这非常有效。我在闭包中移动了“var id”,以消除该参数并稍微简化一些事情。或者你可以像@Tom一样使用循环体作为闭包@Markasoftware:他的建议既不会创建闭包,也不会产生预期的结果。这非常有效。我在闭包中移动了“var id”,以消除该参数并稍微简化一些事情。或者你可以像@Tom一样使用循环体作为闭包@Markasoftware:他的建议既不会创建闭包,也不会产生预期的结果。这非常有效。我在闭包中移动了“var id”以消除该参数并稍微简化。谢谢,这样您就不必担心向闭包发送参数或变量的作用域。谢谢,这样您就不必担心向闭包发送参数或变量的作用域。谢谢,通过这种方式,您不必担心向闭包发送参数或变量的作用域。谢谢,通过这种方式,您不必担心向闭包发送参数或变量的作用域。