Javascript jQuery-for.each调用函数,其中包含方法.on
我需要使用for.each“element jquery method.on”,但我做了一些错误的事情..有人能帮忙吗 从SVG获取ID并将其放入数组: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 =
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();
});