简体   繁体   English

jQuery更好的方法来调用多个元素的函数?

[英]jQuery Better Way to call function on multiple elements?

I'm wondering if there is a better way to handle multiple function calls. 我想知道是否有更好的方法来处理多个函数调用。 Basically I have a function which receives and element as one of the arguments, along with some properties. 基本上我有一个函数接收和元素作为参数之一,以及一些属性。 I have to call this function on several elements... 我必须在几个元素上调用这个函数...

function positionMe(element,position){ ... }

positionMe(obj1,top);
positionMe(obj2,left);
positionMe(obj3,bottom);
positionMe(obj4,right);
positionMe(obj5,bottom);

Just looking at the code seems like I'm doing something really wrong. 只是看代码似乎我做错了什么。 At first I thought I might be able to pass a collection of elements in, but it doesn't seem to work. 起初我以为我可以传递一组元素,但它似乎不起作用。 Any ideas? 有任何想法吗?

With the information you have provided I would create an array with all the needed information and loop through it inside your function: 根据您提供的信息,我将创建一个包含所有必需信息的数组,并在您的函数内循环遍历:

function positionMe(elements)
{
    for(var i = 0, numberOfElements = elements.length; i < numberOfElements ; i++) {
        // Do stuff with the objects you want to do in here

        // access the current object
        console.log(elements[i].object);

        // access the current direction
        console.log(elements[i].direction);
    }
}

var yourObjects = [
    {
        object: obj1,
        direction: 'top'
    },
    {
        object: obj2,
        direction: 'left'
    },
    {
        object: obj3,
        direction: 'bottom'
    },
    // etc
];
positionMe(yourObjects);

UPDATE UPDATE

As Reflective commented : 正如反光评论

better use for (var i in element) { console.log(elements[i].object);} 更好地用于(var i in element){console.log(elements [i] .object);}

This is not the case for arrays in javascript. javascript中的数组不是这种情况。 Well it's not that simple. 那不是那么简单。 because you will end up also looping through all the inherited stuff of the array object like eg pop() , push() etc. There is a way to prevent this though through the use of hasOwnProperty() . 因为你最终还会循环遍历数组对象的所有继承内容,例如pop()push()等。有一种方法可以通过使用hasOwnProperty()来防止这种情况。

Another way would have been to use forEach() which is available as of JavaScript 1.6. 另一种方法是使用从JavaScript 1.6开始提供的forEach() So I don't think all user agents already builtin support for this. 所以我认为所有用户代理都没有内置支持。

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>

var objArray = [
 ["obj1","top"],
 ["obj2","bottom"],
 ["obj3","middle"]
];

$.each(objArray, function(i, v) {positionMe(v[0], v[1]);});

function positionMe(element, val) {
   alert (element+":"+val);
}

</script>

If your objects are elements on the page, you can use jQuery's $.each() function to run the function on each event. 如果您的对象是页面上的元素,则可以使用jQuery的$ .each()函数在每个事件上运行该函数。 The syntax would work like: 语法如下:

$('selector that grabs all objects').each(positionMe($(this), position));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM