簡體   English   中英

單擊后如何只選擇父級中的div

[英]how to select only div within parent after click

我試圖找出如何在單擊事件之后在其父div中選擇具有特定類名的特定元素。

<div id="news">
    <div class="one">
        <p>news</p>
    </div>
    <div class="two"> 
        <a href="#" class="clickme"></a>
    </div>
</div>

<div id="users">
    <div class="one">
        <p>users</p>
    </div>
    <div class="two"> 
        <a href="#" class="clickme"></a>
    </div>
</div>

我簡單的jQuery如下所示:

$(".clickme").on("click", function () {
//how to select only the element with the class "one" within the parent and show it
});

我嘗試失敗的嘗試是嘗試使用父級和最接近的方法以及類似$(this).parents().find(".one").show();

任何幫助將是巨大的!

工作演示

嘗試這個

$(this).parent()引用parent div

.prev('one')到類.one上一個同級

$(this).parent().prev(".one").show();

嘗試使用.prev()

$(this).parent().prev(".one").show();

演示

我會像這樣遍歷它。

$('.clickme').click(function(){
    $(this).parent().siblings().eq(0).show();
});

小提琴

暫無
暫無

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

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