我在jQuery中有一个函数
jQuery(function () {
jQuery(".one").showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();
jQuery(".two").showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();
jQuery(".three").showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();
});现在注意上面的代码。我将showcase函数重复了三次。我想通过数组创建函数一次。怎么可能。我看过jQuery数组的例子,但是看不懂。
发布于 2012-05-14 21:23:21
您可以执行以下操作:
jQuery(function () {
var arObj = ['.one','.two','.three'];
for(var i = 0; i< arObj.length ; i++){
jQuery(arObj[i]).showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();
}
});发布于 2012-05-14 21:20:44
最好的方法不是使用数组,而是将jQuery选择器更改为同时针对所有3个对象;
jQuery(".one,.two,.three").showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();..。它使用multiple selector,尽管您可能想要向这些元素showcase(?)添加另一个类。并将其称为;
jQuery(".showcase").showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();..。而不是。
发布于 2012-05-14 21:24:23
如果你想要一个基于数组的解决方案,你可以这样做:
jQuery.each([".one", ".two", ".three"], function(index, value) {
jQuery(value).showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();
});...or:
function fadeAll(arr) {
jQuery.each(arr, function(index, value) {
jQuery(value).showcase({
animation: { type: "fade" },
titleBar: { autoHide: false },
navigator: { autoHide: true }
});
prettyPrint();
});
}否则就听从马特的回答。
https://stackoverflow.com/questions/10584129
复制相似问题