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

标签 jquery html

我想隐藏所有具有 .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/

相关文章:

jquery - Cycle2 轮播 - 奇怪的行为

javascript - HTML5 视频 OnComplete 下一张幻灯片 Reveal.js

Python正则表达式,匹配太多

html - 屏幕阅读器找不到弹出窗口

javascript - 为什么 jquery.validate.js 会显示两次错误消息?

c# - 您可以使用命名的 JSON 属性将复杂对象传递给 C# Controller 吗?

javascript - 即当鼠标在 div 上时防止鼠标滚轮

php - 如何使用 PHP 将链接的 MySQL 数据插入 <div>

html - 使用媒体查询更改值数据属性

jquery - 如何在jqGrid列中显示不同的值