j查询初学者选择



我有以下html代码:

<div class="first-div">
    <p><a>click this</a></p>
</div>
<br />
<div class="second">
    <p>show this</p>
</div>

我在文档加载时隐藏.second。当我单击唯一的锚点时,我想选择类 second 的div,尝试了所有方法,但仍然没有运气。

这是我的JavaScript:

$(document).ready(function(e) {
    $(".second").hide()
    $("a").click(function(e) {
        $(this).parent().parent().next(".second").toggle()
    });
});

此外,我在 PHP 中动态加载了几个这样的div。

更改,

$(this).parent().parent().next(".second").toggle()

$(this).parent().parent().nextAll(".second").toggle()

next元素不是.second而是br

$(this).closest('.first-div').nextAll(".second").toggle()应该有效。

使用 hide()show()

对于您的链接:

<a href="#" onclick="ShowIt()">Show</a>

在您的代码中:

function ShowIt() {
    $('.second').show()
}

最新更新