我想選擇基於增量ID的列表項,但我有點麻煩。思考?jQuery select增加ID的元素
$(document).ready(function() {
var listCount = 1;
$('#listitem-' + listCount + ' a').hover(function() {
$('#anotheritem-' + listCount).show();
return false;
});
listCount++;
});
HTML:
<ul id="cap">
<li id="listitem-1"><a href="#">content 1</a></li>
<li id="listitem-2"><a href="#">content 2</a></li>
<li id="listitem-3"><a href="#">content 3</a></li>
<li id="listitem-4"><a href="#">content 4</a></li>
</ul>
<div style="display:none;" id="anotheritem-1">hello 1</div>
<div style="display:none;" id="anotheritem-3">hello 3</div>
<div style="display:none;" id="anotheritem-3">hello 3</div>
<div style="display:none;" id="anotheritem-4">hello 4</div>
修訂問題。我試圖達到的回答如下修改:
$('.listitem_to_hover').hover(function() {
var senderID = sender.id.replace('listitem-', '');
$('#anotheritem-' + senderID).show();
}, function() {
var senderID = sender.id.replace('listitem-', '');
$('#anotheritem-' + senderID).hide();
});
什麼樣的麻煩?你可以發佈你的HTML。您的代碼是在for或while循環中?你不能只用一個選擇器呢? – Niklas 2011-05-29 20:14:37
它只是選擇第一個列表項目。我假設我的循環有問題。 – klav 2011-05-29 20:15:25
你可以請張貼你的循環,以及其他與此有關的其他內容。 – Niklas 2011-05-29 20:16:16