gpt4 book ai didi

jquery - 单击相关链接时显示一个 div 并隐藏其他

转载 作者:太空宇宙 更新时间:2023-11-04 14:39:40 25 4
gpt4 key购买 nike

我有这段代码:

<head>
<script>

$(document).ready(function(){
// Optional code to hide all divs
$("div").hide();
// Show chosen div, and hide all others
$("a").click(function ()
{
$("#" + $(this).attr("class")).show().siblings('div').hide();
});

});

</script>

</head>

<body>
Click a button to make it visible:<br /><br />
<a href="" class="one">One</a>
<a href="" class="two">Two</a>
<a href="" class="three">Three</a>
<a href="" class="four">Four</a><br /><br />

<div id="one">One</div>
<div id="two">Two</div>
<div id="three">Three</div>
<div id="four">Four</div><br/><br/>




</body>

我想在点击相关链接时显示一个 div。问题是这个过程只持续几秒钟。有人知道为什么吗?谢谢

最佳答案

使用preventDefault()

        $("a").click(function (e) 
{
e.preventDefault();
$("#" + $(this).attr("class")).show().siblings('div').hide();
});

演示 Fiddle

关于jquery - 单击相关链接时显示一个 div 并隐藏其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843244/

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