gpt4 book ai didi

jquery - 使用 JQuery 隐藏/显示 div 的更简单方法

转载 作者:太空狗 更新时间:2023-10-29 16:36:57 25 4
gpt4 key购买 nike

我想隐藏所有具有 .Hide 类的,然后根据我单击的链接显示特定的 div。到目前为止我已经知道了,但是有没有更简单的方法?

到目前为止我的代码:(使用 JQuery)

$(".btn").click(function (){
$(".Hide").hide("fast");
});

$("#btn_one").click(function () {
$("#one").show("fast");
});

$("#btn_two").click(function () {
$("#two").show("fast");
});

$("#btn_three").click(function () {
$("#three").show("fast");
});

HTML:

<a href="#" id="btn_one" class="btn">one</a>
<a href="#" id="btn_two" class="btn">one</a>
<a href="#" id="btn_three" class="btn">one</a>

<div id="one" class="Hide">1</div>
<div id="two" class="Hide">2</div>
<div id="three" class="Hide">3</div>

最佳答案

数据属性可以是一个选项:

$(".btn").click(function () {
$(".Hide").hide("fast");
$("#" + $(this).data('type')).show("fast");
});

HTML:

<a href="#" id="btn_one" data-type="one" class="btn">one</a>
<a href="#" id="btn_two" data-type="two" class="btn">one</a>
<a href="#" id="btn_three" data-type="three" class="btn">one</a>

您可以使用data-something 来引用相应的元素。

http://jsfiddle.net/dfsq/u8CAD/

关于jquery - 使用 JQuery 隐藏/显示 div 的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384595/

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