jQuery选择ID递增的元素';s
我想根据递增ID选择列表项,但我有点麻烦。想法jQuery选择ID递增的元素';s,jquery,Jquery,我想根据递增ID选择列表项,但我有点麻烦。想法 $(document).ready(function () { var listCount = 1; $('#listitem-' + listCount + ' a').hover(function() { $('#anotheritem-' + listCount).show(); return false; }); listCount++; }); HTM
$(document).ready(function () {
var listCount = 1;
$('#listitem-' + listCount + ' a').hover(function() {
$('#anotheritem-' + listCount).show();
return false;
});
listCount++;
});
HTML:
如果看不到所有的代码,很难确定,但是您是否可以将所有列表项设置为同一类?例如
然后
因此,您正在循环(或尝试循环)以分配悬停函数,为什么不使用类选择器呢
//changing # to . makes jquery select ALL of the elements with that class assigned.
$('.listitem_to_hover').hover(function() {
$(this).toggleClass('current');
return false;
});
HTML:
ps:即使你编辑了你的文档。准备好了吗。。。你没有回路
*好的,根据你所展示的,这应该得到你想要的:*
$(文档).ready(函数(){
$('.listitem_to_hover').hover(函数(){showItem(this);},函数(){hideItem(this);})
});
函数showItem(发送方){
var senderID=sender.id.replace('listitem-','');
$('#anotheritem-'+senderID).fadeIn();/.removeClass('hidden');
}
函数hideItem(发送方){
var senderID=sender.id.replace('listitem-','');
$('#anotheritem-'+senderID).fadeOut();/.addClass('hidden');
}
隐藏的
{
显示:无;
}
你好1
你好2
你好3
你好4
您可以使用父ul作为选择器
$('#cap li a').hover(function() {
$(this).toggleClass('current');
});
我不会在每个列表项中添加一个类,而是通过ul选择它们
$('#cap li a').hover(function() {
$(this).toggleClass('current');
return false;
});
什么样的麻烦?你能发布你的html吗。您的代码是在for还是while循环中?您不能只使用选择器,它只是选择第一个列表项。我猜我的循环有问题。你能发布你的循环,以及其他与此相关的东西吗?你呈现它的方式是一个谜。请提供您的脚本和html的更多部分。这里是html,谢谢您的回复。我刚刚更新了我的原始条目。基于此-如果您将class=“foo”添加到每个li元素中,关于的代码应该可以工作,因为我最终将修改代码以选择另一个ID。例如#listitem-1将显示()#hiddenimage-1Ok,这不重要。如果您想“更改代码,以便当您将鼠标悬停在列表项上时,隐藏的图像以id显示”,则可以按类分配悬停函数,并且仍然可以在处理程序中标识源(特定li)的id,然后在处理程序中显示相应的图像。是,但相应的图像将不会位于LI内。我已经更新了我原来的帖子,以提供更多信息detail@klav,我已经更新了答案。。你看,你不需要让外部目标在同一个LI内就能得到你想要的结果。@Cos Callis:啊,我明白了。正是我需要的!万分感谢!
//changing # to . makes jquery select ALL of the elements with that class assigned.
$('.listitem_to_hover').hover(function() {
$(this).toggleClass('current');
return false;
});
<ul id="cap">
<li id="listitem-1" class="listitem_to_hover"><a href="#">content 1</a></li>
<li id="listitem-2" class="listitem_to_hover"><a href="#">content 2</a></li>
<li id="listitem-3" class="listitem_to_hover"><a href="#">content 3</a></li>
<li id="listitem-4" class="listitem_to_hover"><a href="#">content 4</a></li>
</ul>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.listitem_to_hover').hover(function () { showItem(this); }, function () { hideItem(this); })
});
function showItem(sender) {
var senderID = sender.id.replace('listitem-', '');
$('#anotheritem-' + senderID).fadeIn();// .removeClass('hidden');
}
function hideItem(sender) {
var senderID = sender.id.replace('listitem-', '');
$('#anotheritem-' + senderID).fadeOut(); //.addClass('hidden');
}
</script>
<style type="text/css">
.hidden
{
display: none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<ul id="cap">
<li id="listitem-1" class="listitem_to_hover"><a href="#">content 1</a></li>
<li id="listitem-2" class="listitem_to_hover"><a href="#">content 2</a></li>
<li id="listitem-3" class="listitem_to_hover"><a href="#">content 3</a></li>
<li id="listitem-4" class="listitem_to_hover"><a href="#">content 4</a></li>
</ul>
<div class="hidden" id="anotheritem-1">
hello 1</div>
<div class="hidden" id="anotheritem-2">
hello 2</div>
<div class="hidden" id="anotheritem-3">
hello 3</div>
<div class="hidden" id="anotheritem-4">
hello 4</div>
</div>
</form>
</body>
</html>
$('#cap li a').hover(function() {
$(this).toggleClass('current');
});
$('#cap li a').hover(function() {
$(this).toggleClass('current');
return false;
});