简体   繁体   English

单击链接时显示子div

[英]Show a child div when link clicked

I have a series of divs with links and hidden divs. 我有一系列带有链接和隐藏div的div。 When the link is clicked, I need the hidden div within the parent to be shown. 单击链接时,我需要显示父级中的隐藏div。 For example: 例如:

<div class="track" id="track1">
<a href="#" class="notes-link">Song Notes</a>
<div class="song-notes"></div> <!-- HIDDEN DIV TO BE SHOWN WHEN LINK CLICKED -->
</div>

<div class="track" id="track2">
<a href="#" class="notes-link">Song Notes</a>
<div class="song-notes"></div> <!-- HIDDEN DIV TO BE SHOWN WHEN LINK CLICKED -->
</div>

So far, the option that I tried opened up ALL of the .song-notes divs, but I want to specify that it's only the child divs of the parent div where the link is contained that should be opened. 到目前为止,我尝试的选项打开了所有.song-notes div,但我想指定它只是包含应该打开的链接的父div的子div。

I like using .toggle() here. 我喜欢在这里使用.toggle()

$('a.notes-link').click(function (){
    $(this).next('.song-notes').toggle();
});

jsFiddle example jsFiddle例子

try this 尝试这个

$(document).ready(function(){
    $('.notes-link').click(function(ev){
       ev.preventDefault();
       $(this).closest('.track').find('.song-notes').show();
    });
});

DEMO DEMO

You can do this: 你可以这样做:

$(function () {
    $('.notes-link').click(function (e) {
        e.preventDefault();
        $(this).next().show();
    });
});

Demo 演示

$(function () {
    $('div.track a.notes-link').click(function (e) {
        e.preventDefault();
        $(this).next().show(); //$(this).next().toggle();--> to show hide
    });
});

.next() 。下一个()

.toggle() .toggle()

Add id to the hidden divs 将id添加到隐藏的div

<div class="track" id="track1">
<a href="javascript:showNotes(1);" class="notes-link">Song Notes</a>
<div class="song-notes" style="display:none" id="song1">1</div> <!-- HIDDEN DIV TO BE SHOWN WHEN LINK CLICKED -->
</div>

<div class="track" id="track2">
<a href="javascript:showNotes(2);" class="notes-link">Song Notes</a>
<div class="song-notes" style="display:none" id="song2">2</div> <!-- HIDDEN DIV TO BE SHOWN WHEN LINK CLICKED -->
</div>

Javascript Code: Javascript代码:

    function showNotes(id){
        $('#song'+id).toggle();
    }

Demo: http://jsfiddle.net/Zz65U/ 演示: http//jsfiddle.net/Zz65U/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM