gpt4 book ai didi

jquery - 新手 切换问题

转载 作者:行者123 更新时间:2023-12-01 05:06:12 26 4
gpt4 key购买 nike

我在这里缺少什么?当我单击“h2 a”链接时,.content ol 应该切换。我不明白为什么它不起作用:(

<script type="text/javascript">
$(document).ready(function(){
$(".content ol").hide();
$(".content h2 a").click(function(){
$(this).find(".content ol").toggle(400);
});
});
</script>

<div class="content">
<h2><a href="#">click me</a></h2>
<ol>
<p>Some text</p>
</ol>
</div>

最佳答案

问题是 find 查找当前选定元素的后代元素。您正在this 中查找,这是被单击的链接。显然,ol 不在 a 元素内。

您需要使用closest ,要沿着树向上到达 div,并且仅然后使用 find:

$(".content h2 a").click(function(){
$(this).closest('.content').find("ol").toggle(400);
});

关于jquery - 新手 切换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com