簡體   English   中英

切換特定的li div

[英]Toggle specific li div

<li><a href="http://www.yahoo.com " target="_blank"> goto yahoo.</a>
<span id="comment"><img src="https://dl.dropboxusercontent.com/u/26715029/img/fc.jpg"       style="vertical-align:middle" width="20" height="20" alt="fc"/>| or comment about yahoo</span>
<div class="commentbox">
<div id="fb-root">coment box is here</div>

</div>

</li>
<li><a href="http://www.google.com " target="_blank"> go to google </a>
<span id="comment"><img src="https://dl.dropboxusercontent.com/u/26715029/img/fc.jpg"  style="vertical-align:middle" width="20" height="20" alt="fc"/>or comment about google</span>
<div class="commentbox">

</div>

</li>

需要切換特定的li div,而不是兩者都切換。 嘗試過手風琴,但鏈接不起作用。 僅供參考:列表更大http://jsfiddle.net/NQw5L/18/

ID 必須是唯一的。 如果將它們轉換為類,則可以使用:

$(document).ready(function () {
    $('.comment').click(function () {
        $(this).next('.commentbox').slideToggle("slow");
    });
});

jsFiddle示例

j08691是正確的,但是如果您不想手動執行-嘗試設置類而不是ID並使用以下方法:

$(document).ready(function() {
    var cCtr=0;
    $('.commentbox').each (function() {
       $(this).attr('id','comment'+cCtr++); 
    });
    var cCtr=0;
    $('.comment').each (function() {
       $(this).attr('data-targ', 'comment'+cCtr++);
    });

    $('.comment').click(function() {
        var slId = $(this).attr('data-targ');
        $('#'+slId).slideToggle("slow");

    });
}); 

http://jsfiddle.net/RHsxG/3/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM