使用jquery/javascript在n个元素中选择备用框
我正在做一个项目,以使用jquery/javascript在n个元素中选择备用框,javascript,jquery,Javascript,Jquery,我正在做一个项目,以json对象的形式获取数据。我在下面描述的框中完美地显示数据(当前)。但我想为不同颜色的可选框添加类,如下所示(必需) 代码: jQuery.each(Stories, function (index) { var lindex = 6 * PageID; var findex = lindex - 6; if (index + 1 > findex && index + 1 <= lindex) {
json
对象的形式获取数据。我在下面描述的框中完美地显示数据(当前)。但我想为不同颜色的可选框添加类,如下所示(必需)
代码:
jQuery.each(Stories, function (index) {
var lindex = 6 * PageID;
var findex = lindex - 6;
if (index + 1 > findex && index + 1 <= lindex) {
var stories = dvStoreies;
if ((index) % 3 == 0) {
stories.append("<div class='dvStoriesFirst' style='clear:left;float:left;margin-bottom:40px;' ID=story_" + index + "></div>");
}
else {
stories.append("<div class='dvStories' style='float:left;margin-bottom:40px;' ID=story_" + index + "></div>");
}
}
});
<div class="stories">
<div class="dvStoriesFirst" id="s_box1"></div>
<div class="dvStories" id="s_box2"></div>
<div class="dvStories" id="s_box3"></div>
<div class="dvStoriesFirst" id="s_box4"></div>
<div class="dvStories" id="s_box5"></div>
<div class="dvStories" id="s_box6"></div>
</div>
jQuery.each(故事、函数(索引){
var-lindex=6*PageID;
var-findex=lindex-6;
如果(index+1>findex&&index+1按照从左到右一行接一行的顺序添加方框,则测试方框编号模3模2是否为0应该很简单,即:
var shouldHighlight = (zeroBasedBoxNumber % 3 % 2) === 0;
因此,对于您的代码(以及缺少的一些ID和css类):
jQuery.each(故事、函数(索引、故事){
var style=['float:left','margin-bottom:40px'];
if(索引%3==0)style.push('clear:left');
if(索引%3%2==0)style.push('background-color:#aaa');
附加(“”);
});
如果您的盒子是按此顺序从左到右依次添加的
一行接一行,它应该像测试盒子编号模3模2是否为0一样简单,即:
var shouldHighlight = (zeroBasedBoxNumber % 3 % 2) === 0;
因此,对于您的代码(以及缺少的一些ID和css类):
jQuery.each(故事、函数(索引、故事){
var style=['float:left','margin-bottom:40px'];
if(索引%3==0)style.push('clear:left');
if(索引%3%2==0)style.push('background-color:#aaa');
附加(“”);
});
如果您希望交替应用该类,则应
if ((index) % 3 % 2 == 0)
希望对您有所帮助如果您想选择应用该课程,应该
if ((index) % 3 % 2 == 0)
希望对您有所帮助以下是一个可行的解决方案
CSS
.dvStories{
width:100px;height:100px;
background:grey;
margin:2px;
float:left;
}
.orangeBG{
background:orange;
}
希望这对你有所帮助这里有一个可行的方法
CSS
.dvStories{
width:100px;height:100px;
background:grey;
margin:2px;
float:left;
}
.orangeBG{
background:orange;
}
希望这对你有所帮助你能发布一个静态json的补丁吗?不能发布dude.html?或任何解析数据的url。你能发布一个静态json的补丁吗?不能发布dude.html?或任何解析数据的url。