我正在尝试循环遍历无序列表的元素,以便根据click事件一次只显示两个元素。我可以通过显示/隐藏元素来实现这一目标,但这似乎限制了我只有四个项目,我有六个项目,并且会添加更多项目。
我认为jQuery .each()函数应该可以循环并切换显示属性,但我仍然坚持从哪里开始。任何帮助表示赞赏。谢谢。这就是我想要解决的问题。
LinearLayout layout = new LinearLayout(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setWeightSum(lessons);
for (int i = 0; i < lessons; i++) {
TextView text = new TextView(context);
LayoutParams textParams = new LayoutParams((int) (0 * (context.getResources().getDisplayMetrics().density) + 0.5f),
(int) (length[i] * (context.getResources().getDisplayMetrics().density) + 0.5f), 1);
text.setLayoutParams(textParams);
System.out.println(length[i]);
text.setPadding((int) (8 * (context.getResources().getDisplayMetrics().density) + 0.5f), 0, 0, 0);
text.setTextColor(context.getResources().getColor(R.color.white_text));
text.setBackgroundColor(context.getResources().getColor(R.color.class.getField(name[i]).getInt(null)));
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
text.setGravity(Gravity.CENTER_VERTICAL);
text.setText(R.string.class.getField(name[i]).getInt(null));
layout.addView(text);
}
week[day].addView(layout);
答案 0 :(得分:1)
$('#buttonClick').on('click', function() {
var showing = $(this).closest('.thumbBrowser').find('ul li:visible');
var next = showing.last().next();
if( next.length === 0 ) {
next = $(this).closest('.thumbBrowser').find('ul li').first();
}
next.toggleClass('hidden').next().toggleClass('hidden');
showing.toggleClass('hidden');
});
&#13;
.hidden {
display:none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="thumbBrowser">
<ul>
<li class="thumbLeft caseStudy tint tintWhite">
<a href="client-page.html"><img src="images/argus_thumb.jpg" alt="one"></a>
</li>
<li class="thumbRight caseStudy tint">
<img src="images/adr_thumb.jpg" alt="two">
</li>
<li class="hidden thumbLeft caseStudy tint tintWhite">
<img src="images/dd_thumb.jpg" alt="three">
</li>
<li class="hidden thumbRight caseStudy tint">
<img src="images/cdp_thumb.jpg" alt="four">
</li>
<li class="hidden thumbRight caseStudy tint tintWhite">
<a href="client-page.html"><img src="images/pm_thumb.jpg" alt="five"></a>
</li>
<li class="hidden thumbLeft caseStudy tint tintWhite">
<img src="images/argus_thumb.jpg" alt="six">
</li>
</ul>
<div class="cycleButton" id="buttonClick"><img src="images/cycleIcon.png"></div>
</div>
&#13;
答案 1 :(得分:1)
抱歉,我没有给你写任何代码,但我认为它应该足够清楚,你应该没有问题在谷歌搜索丢失信息的较小部分(比如如何获得当前项目的索引和例如)
答案 2 :(得分:0)
我会做以下事情:
class User(AbstractBaseUser):
username = models.CharField(max_length=100,unique = True, null = False, blank = True)
agent = models.CharField(max_length = 1000, null = False, blank = False)
platform = models.CharField(max_length = 100,null = True, blank = True)
screen_width = models.IntegerField(blank = True, null=True)
screen_height = models.IntegerField(blank = True, null=True)
screen_resolution = models.IntegerField(blank = True, null=True)
browser = models.CharField(max_length = 100,blank = True)
is_active = models.BooleanField(default = True, null = False)
is_staff = models.BooleanField(default = False, null = False)
# Adds the date when the user was first created
date_joined = models.DateTimeField(auto_now_add = True,null = False)
# Updates the object every time is is saved
last_updated = models.DateTimeField(auto_now = True, null = False)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['agent']
class UserManager(BaseUserManager):
def create_user(self,**kwargs):
"""
Creates and saves a User
"""
last_user = get_user_model().objects.all().last()
# For first user edge case
if not last_user:
username = "anon_1"
else:
username = "anon_" + str(last_user.id + 1)
kwargs["username"] = username
user = get_user_model().objects.create(**kwargs)
return user
def create_superuser(self, username,password):
"""
Creates and saves a Superuser with the given username, gender and password.
"""
user = self.create_user(
username = username
)
user.is_staff = True
user.is_superuser = True
user.set_password(password)
user.save(using = self._db)
return user
选择器过滤该列表以获取当前可见项目的数量:visible
函数选择接下来的两个项目并显示它们这是一个正在运行的例子:
.slice()
$(".cycleButton").on("click", function() {
var items = $(".thumbBrowser li");
var shownItems = items.filter(":visible").length;
items.slice(shownItems, shownItems + 2).show(); // you could use addClass("hidden") too
});
.hidden { display : none };