javascript - 通过javascript同时更改css的最佳方法?
问题描述
我有这个 HTML。
<div class="rankings-wrapper">
<div class="top-header">
<a id="qb" href="#">QB</a>
<a id="rb" href="#">RB</a>
<a id="wr" href="#">WR</a>
<a id="te" href="#">TE</a>
<a id="dst" href="#">D/ST</a>
</div>
<div class="player-info-wrapper">
<div id="qb-wrapper" class="qb-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>QBs and other various football terms. QBs and other various football terms. QBs and other various football terms.
QBs and other various football terms. QBs and other various football terms. QBs and other various football terms.</p>
</div>
<div id="rb-wrapper" class="rb-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>RBs and other various football terms. RBs and other various football terms. RBs and other various football terms.
RBs and other various football terms. RBs and other various football terms. RBs and other various football terms.</p>
</div>
</div>
<div id="wr-wrapper" class="wr-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>WRs and other various football terms. WRs and other various football terms. WRs and other various football terms.
WRs and other various football terms. WRs and other various football terms. WRs and other various football terms.</p>
</div>
</div>
<div id="te-wrapper" class="te-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>TEs and other various football terms. TEs and other various football terms. TEs and other various football terms.
TEs and other various football terms. TEs and other various football terms. TEs and other various football terms.</p>
</div>
<div id="dst-wrapper" class="dst-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>D/STs and other various football terms. D/STs and other various football terms. D/STs and other various football terms.
D/STs and other various football terms. D/STs and other various football terms. D/STs and other various football terms.</p>
</div>
</div>
我有一个排名框,我希望我的 CSS 仅在我单击标题中的 QB 时显示 QB 信息,并隐藏其余部分。当我单击 RB、TE 等时,我也希望发生同样的事情。通过 Javascript 执行此操作的最简单方法是什么?
解决方案
你可以用几行 Jquery 来做到这一点。
首先,您应该隐藏所有包装器,您可以通过使用 .hide-wrappers 向所有元素添加一个额外的类来轻松做到这一点,这样您就可以一次隐藏所有它们。
之后,您必须为此添加一个单击事件到所有 URL,单击事件将触发一些额外的 JQuery,仅显示所需的 Wrapper。
<script>
$('.hide-wrapper').hide();
$('.top-header a').click(function(){
$('.hide-wrapper').hide();
var wrapperId = $(this).attr('id');
$('.'+wrapperId+'-wrapper').show();
});
</script>
<div class="rankings-wrapper">
<div class="top-header">
<a id="qb" href="#">QB</a>
<a id="rb" href="#">RB</a>
<a id="wr" href="#">WR</a>
<a id="te" href="#">TE</a>
<a id="dst" href="#">D/ST</a>
</div>
</div>
<div class="player-info-wrapper">
<div id="qb-wrapper" class="qb-wrapper hide-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>QBs and other various football terms. QBs and other various football terms. QBs and other various football terms.
QBs and other various football terms. QBs and other various football terms. QBs and other various football terms.</p>
</div>
</div>
<div id="rb-wrapper" class="rb-wrapper hide-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>RBs and other various football terms. RBs and other various football terms. RBs and other various football terms.
RBs and other various football terms. RBs and other various football terms. RBs and other various football terms.</p>
</div>
</div>
<div id="wr-wrapper" class="wr-wrapper hide-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>WRs and other various football terms. WRs and other various football terms. WRs and other various football terms.
WRs and other various football terms. WRs and other various football terms. WRs and other various football terms.</p>
</div>
</div>
<div id="te-wrapper" class="te-wrapper hide-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>TEs and other various football terms. TEs and other various football terms. TEs and other various football terms.
TEs and other various football terms. TEs and other various football terms. TEs and other various football terms.</p>
</div>
</div>
<div id="dst-wrapper" class="dst-wrapper hide-wrapper">
<div class="player-info">
<h4>Player Name</h4>
<p>D/STs and other various football terms. D/STs and other various football terms. D/STs and other various football terms.
D/STs and other various football terms. D/STs and other various football terms. D/STs and other various football terms.</p>
</div>
</div>
</div>
推荐阅读
- git - 适用于我公司所有开发团队的 Git 分支策略
- javascript - 反应重新渲染待办事项数组
- python-3.x - 如何打开 Raspberry Pi 上的 OLED 显示屏?
- node.js - Mongoose.aggregate(pipeline) 使用 $unwind、$lookup、$group 链接多个集合
- angular - 遍历打字稿中的地图对象列表
- java - 为什么文件中的数组对象在 Java 中有 NumberFormatException?
- java - @RequestMapping 找不到 Jsp 页面
- node.js - 节点 js 应用引擎 github 触发器构建未发布到开发?
- html - 如何获取 HTML 中的字体大小?
- flutter - 如何将文件转换回资产