我想隐藏所有具有 .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
来引用相应的元素。
关于jquery - 使用 JQuery 隐藏/显示 div 的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384595/