簡體   English   中英

單擊即可使用Slidetoggle

[英]Slidetoggle next using on click

我在頁面上有這些的倍數,我只想在每個區域內滑動“注釋框”(即:它找到的第一個)。

<div class="comments">
<a style="cursor:pointer" onclick="jQuery('.comments-box').slideToggle();">comments</a></span>
        <div class="add-comment">add</div>
        <div class="response-form"></div>
        <div class="comments-box">
         a bunch of comments
        </div>
</div>

但是,這顯然會切換每個“評論框”。

我已經嘗試過onclick="jQuery($(this).next('.comments-box')).slideToggle();" 但沒有運氣。

更改標記,刪除內聯JavaScript,然后在錨點中添加href:

<div class="comments">
    <span><a href="#">comments</a></span>
    <div class="add-comment">add</div>
    <div class="response-form"></div>
    <div class="comments-box">
         a bunch of comments
    </div>
</div>

然后執行以下操作,使每個注釋框上下滑動:

$(function() {
    $('.comments a').on('click', function(e) {
        e.preventDefault();
        $(this).closest('.comments').find('.comments-box').slideToggle();
    });
});

編輯:

內聯:

<a onclick="jQuery(this).closest('.comments').find('.comments-box').slideToggle();">comments</a>

嘗試這個'

$('.comments a').on("click",function(){
        $(this).parent().find('.comments-box').slideToggle();

        });

FIDDLE示范

您也可以使用siblings() 嘗試這個

$(function() {
    $('.comments a').on('click', function(e) {
        e.preventDefault();
        $(this).parent().siblings('.comments-box').slideToggle();
    });
});

JSfiddle演示

暫無
暫無

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

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