jquery fancybox插件在代码中使用数组? [英] Jquery fancybox plugin using arrays in code?
问题描述
$ 我有一个用于fancybox的jquery插件,当我使用下面的代码时, (a#roomthumb_2237)。fancybox({
'href':'#2237_Info',
'titleShow':false,
'transitionIn':'elastic',
'transitionOut':'elastic'
});
但是为了阻止我不得不多次重复这个代码(使用不同的href值)喜欢在for循环中运行它。
我试过的代码是:
for(var i = 0; i< rooms_array.length; i ++){
var d =#roomthumb _+ rooms_array [i]
$(d).fancybox( {
'href':rooms_array [i] +'_ Info',
'titleShow':false,
'transitionIn':'elastic',
'transitionOut':'elastic '
});
};
原来的代码我直接从谷歌(这可能是从stackoverflow,我不能记住),但只要我试图把它放在循环中,我就迷路了,因为我不知道该怎么写。我认为href行是错误的,但我找不到解释如何做我需要的东西。
我不知道如何编写$( a#roomthumb_2237)as $(a+ d)例如...
我解决了我自己的问题。我的循环是好的,唯一的文本是错误的,这是我认为是:
'href': rooms_array [i] +'_ Info',
我需要:
'href':'#'+ rooms_array [i] +'_ Info',
$ p $现在我已经注意到了,真的很明显...我没有添加选择器。
em>编辑:
这里是我的完整代码,现在可以工作。注意 - 我已经为我的数组选择了随机数,只是为了显示这个想法:
$ p $ code var room_array = [2235,2236, 2237,2238];
var reposition = function(){
$('#hidden_stuff')。attr('display',none);
$ .each(rooms_array,function(key,value){
var a ='#roomthumb _'+ value;
var b ='#chosenrate _'+ value; $ ($(b).length){
var c = $(b).offset();
$(a).css({
可见度:可见,
top:c.top -9 +px,
left:c.left + 560 +px
})
}
$ {
}
$(a).fancybox({
'href':'#'+ value +'_ Info',
' titleShow':false,
'transitionIn':'elastic',
'transitionOut':'elastic'
});
});
};
I've got a Jquery plugin for a fancybox, which I know works when I use the following code:
$("a#roomthumb_2237").fancybox({
'href' : '#2237_Info',
'titleShow' : false,
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
});
But to stop me having to repeat this code multiple times (with different href values), I'd like to run it in a for loop.
The code I've tried is:
for (var i = 0; i < rooms_array.length; i++) {
var d = "#roomthumb_"+rooms_array[i]
$(d).fancybox({
'href' : rooms_array[i]+'_Info',
'titleShow' : false,
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
});
};
The original code I took straight from google (it may well have been from stackoverflow, I can't remember), but as soon as I try to put it the loop, I'm lost, since I've no idea how to write it. I think the href line is wrong, but I can't find anything that explains how to do what I need it to.
I dont know how to write the $("a#roomthumb_2237") as $("a"+d) for example...
I fixed my own issue. My loop was fine, the only line of text that was wrong, which is what I thought, was:
'href' : rooms_array[i]+'_Info',
I needed to be:
'href' : '#'+rooms_array[i]+'_Info',
Which now I've noticed, is really obvious... I hadn't added the selector.
Edit: Here is my full code, which now works. Note - I've chosen random numbers for my array, just to show the idea:
var rooms_array = [2235, 2236, 2237, 2238];
var reposition = function (){
$('#hidden_stuff').attr('display', "none");
$.each(rooms_array, function(key,value){
var a = '#roomthumb_'+value;
var b = '#chosenrate_'+value;
if ($(b).length) {
var c = $(b).offset();
$(a).css({
visibility:"visible",
top: c.top -9 + "px",
left: c.left + 560 + "px"
})
}
else{
}
$(a).fancybox({
'href' : '#'+value+'_Info',
'titleShow' : false,
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
});
});
};
这篇关于jquery fancybox插件在代码中使用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!