Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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 jQuery-for.each调用函数,其中包含方法.on_Javascript_Jquery - Fatal编程技术网

Javascript jQuery-for.each调用函数,其中包含方法.on

Javascript jQuery-for.each调用函数,其中包含方法.on,javascript,jquery,Javascript,Jquery,我需要使用for.each“element jquery method.on”,但我做了一些错误的事情..有人能帮忙吗 从SVG获取ID并将其放入数组: var svgZi10 = document.getElementById("svg"); //get the main SVG container var svgElementZi10 = svgZi10.contentDocument; //get the inner DOM from SVG var lock =

我需要使用for.each“element jquery method.on”,但我做了一些错误的事情..有人能帮忙吗

从SVG获取ID并将其放入数组:

    var svgZi10 = document.getElementById("svg"); //get the main SVG container
    var svgElementZi10 = svgZi10.contentDocument; //get the inner DOM from SVG

var lock    = []; //new Array
lock[0] = svgElementZi10.getElementById("lock-door-open-right-contour"); //get the inner element by id
lock[1] =  svgElementZi10.getElementById("lock-door-open-left-contour"); //get the inner element by id
lock[2] = svgElementZi10.getElementById("lock-door-closed-right-contour"); //get the inner element by id
lock[3] = svgElementZi10.getElementById("lock-door-closed-left-contour"); //get the inner element by id
使用数组中的对象进行操作(不起作用):

每个对象何时单独(工作):


应该对当前元素而不是数组调用
.on()

$.each(lock, function(i,element) {
    $(element).on("mousedown",function(){tab12[0].click() })  
});
如果
lock
是DOM元素的数组,则不需要
。每个
。当您给
jQuery()
一个元素数组时,它将创建一个包装它们的jQuery对象。所以你可以写:

$(lock).on("mousedown", function() {
    tab12[0].click();
});

在jQuery集合上使用绑定函数时,它会绑定集合中的所有元素。

method.on不可能用于Array?这是可能的,但为什么每次都要通过循环执行?使用
.each()
处理单个元素的全部要点。
锁的元素是什么?它们是jQuery对象还是DOM元素?lock是SVGI中的DOM元素数组,根据第二个示例,我试图使用它,但仍然没有执行该函数
$.each(lock, function(i,element) {
    $(element).on("mousedown",function(){tab12[0].click() })  
});
$(lock).on("mousedown", function() {
    tab12[0].click();
});