我有一个jQuery插件的功能,可循环浏览某些图像。
//load images
function ImageSettings(config, fileNames) {
//loop through the array in the second argument
for (var x = 0; x < fileNames.length; x++) {
//create image
$("<img />").attr({
id: fileNames[x],
src: config.imgDir + fileNames[x] + config.imgFormat,
title: fileNames[x] + " layout"
}).appendTo("#" + config.imgContainer);
}
};
在插件的更下方,我需要将图像attr ID传递到无序列表项中,但是我的变量位于名为fileNames的函数中。
因此,如果使用:
$(config.sidebar).append('<ul>' + fileNames + '</ul>');
//I get a string like: home, about, contact but I need it to be styled in a list item
如果我使用split方法删除“”,则将得到未知的方法拆分。那么,是否可以在内部传递函数和变量?例如,这样我就可以解决未知方法了吗?
$(config.sidebar).append('<ul>' +
ImageSettings(fileNames[x]).each(function() {
$( this ).wrap( "<li>" );
+ '</ul>');
我考虑过使用$('img')。attr('id')之类的东西,然后在列表项中设置样式,但是因为我的页面上将有几张图像,但并不是全部都将被加载,并且并非全部都需要包装在列表项中。因此,我想在函数内使用变量的原因。谢谢你。
您的代码似乎有些混乱。请问这个解决问题了吗?
这是代码:
var config = {
"imgContainer": "imgContainer",
"sidebar": "ul",
"imgDir": "",
"imgFormat": "jpg"
};
var fNames= [/* list of file names */];
function ImageSettings(fileNames) {
//loop through the array in the second argument
for (var x = 0; x < fileNames.length; x++) {
var fname = fileNames[x];
//create image
$("<img />").attr({
id: fname.substr(fname.lastIndexOf("/"+1, fname.length)),
src: config.imgDir + fname + "."+config.imgFormat,
title: fname.substr(fname.lastIndexOf("/"+1, fname.length)) + " layout"
}).appendTo("#" + config.imgContainer);
$(config.sidebar).append('<li>' + fname + '</li>');
}
}
ImageSettings(fNames);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句