我有以下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()
}