javascript 将 jQuery 函数应用于具有相同类的多个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12490659/
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
Apply jQuery function to multiple elements with same class
提问by Liam
I have a web page that has multiple sliders on, all with the class '.viewer'
我有一个带有多个滑块的网页,所有滑块都带有“.viewer”类
If I add my jQuery
如果我添加我的 jQuery
$('.viewer').carousel('.viewer #simplePrevious', '.viewer #simpleNext');
This doesnt work, Is this because it doesnt know what slider this applies too? Should it not apply to all .viewer elements on the page?
这不起作用,这是因为它不知道这也适用于哪个滑块?它不应该应用于页面上的所有 .viewer 元素吗?
回答by Littm
You should use the jQuery method each()
:
您应该使用 jQuery 方法each()
:
$('.viewer').each(function() {
$(this).carousel('.viewer #simplePrevious', '.viewer #simpleNext');
});
Check the online doc for more information: http://api.jquery.com/each/
查看在线文档了解更多信息:http: //api.jquery.com/each/
Hope this helps mate.
希望这有助于队友。