javascript jQuery 在所有类元素上触发函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8520870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery fire function on all class elements
提问by Peter
I want to execute some function on all class elements. How can i do that?
我想对所有类元素执行一些函数。我怎样才能做到这一点?
what i want is something like:
我想要的是这样的:
// i want to fire my function on all class elements
$('.myClass').go(function(){
// my function using "this" object
});
i know this is a dumb question, i used to work on propotype for some time and now i don't remember proper function for jquery
我知道这是一个愚蠢的问题,我曾经在 propotype 上工作过一段时间,现在我不记得 jquery 的正确功能
回答by karim79
Simple:
简单的:
$(".myClass").each(function() {
...
});
回答by Shadow Wizard is Ear For You
Just use the common .each()
iterator:
只需使用通用.each()
迭代器:
$('.myClass').each(function() {
go($(this));
});
Another option if you're into more elegant code, write your own plugin - good answer can be found in this question: How to create a jQuery plugin with methods?
如果您喜欢更优雅的代码,另一种选择是编写自己的插件 - 可以在以下问题中找到好的答案:如何使用方法创建 jQuery 插件?