javascript - 让地址链接在 JavaScript 中打开隐藏部分
问题描述
我是 JS 的新手,所以我的问题是,如果我有很多部分,我可以在单击按钮时打开它们(display: none;
默认情况下,它们是在单击同一页面上的菜单按钮时打开的)。我需要创建链接,如果有人在地址框中输入,那么该部分将打开......但是如果它display: none;
是按地址创建的,如何做到这一点?也许我错了,您可以提出一些更好的方法吗?
<section style="display: none;" id="sectionIepirkumi" class="w3-container w3-threequarter section">
<div id="sectionTopLine" class="w3-border-top w3-border-green"></div>
<h5 id="sectionAddress" class="w3-margin-bottom">
<a id="sectionAddressFirstPageLink" class="sectionAddressLink" onclick="openSection('sectionFirstPage')">
Sākumlapa </a>/
<a class="sectionAddressLink" onclick="openSection('sectionAboutSchoolPage')"> Par Skolu </a>/
<a class="" style="font-weight: bold;"> IEPIRKUMI</a>
</h5>
<div id="sectionBottomLine" class="w3-border-top w3-border-green w3-margin-bottom"></div>
<div class="w3-panel w3-border">
<h3>Uz doto momentu nav aktuālu iepirkumu!</h3>
</div>
</section>
function myFunction(id) {
var x = document.getElementById(id);
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
解决方案
你让它变得复杂,因为它(而不是使用if..else)有一个classList的方法,它可以让你切换你想要的元素的类。该方法是toggle()。
稍后您可以创建一个名为的类,该类将在将类赋予元素时将.show
元素的显示更改为阻塞。
提醒:永远不要使用var
, 使用let & const
最新的语法。( ecma 脚本 6 )
function myFunction() {
const container = document.getElementById('sectionIepirkumi');
container.classList.toggle('show');
};
document.querySelector('button').addEventListener('click', myFunction);
.show {
display: block !important;
}
<button type='button'>Click</button>
<section style="display: none;" id="sectionIepirkumi" class="w3-container w3-threequarter section">
<div id="sectionTopLine" class="w3-border-top w3-border-green"></div>
<h5 id="sectionAddress" class="w3-margin-bottom">
<a id="sectionAddressFirstPageLink" class="sectionAddressLink" onclick="openSection('sectionFirstPage')">
Sākumlapa </a>/
<a class="sectionAddressLink" onclick="openSection('sectionAboutSchoolPage')"> Par Skolu </a>/
<a class="" style="font-weight: bold;"> IEPIRKUMI</a>
</h5>
<div id="sectionBottomLine" class="w3-border-top w3-border-green w3-margin-bottom"></div>
<div class="w3-panel w3-border">
<h3>Uz doto momentu nav aktuālu iepirkumu!</h3>
</div>
</section>
推荐阅读
- linux - 比较日志中的纪元时间戳并检查列表中的前一个时间戳和当前时间戳之间是否经过 x 时间
- angular - 如何加载具有默认值的四个角材质选择框?
- python - 如何创建最后一列(即第五列)将添加前四列的 DataFrame
- html - 我是否必须再次为多个 html 页面导入(重复)src 库才能获得它们的功能?
- python - 没有函数匹配给定的名称和参数类型。在带有 python 的 postgres 中
- java - AssertJ 可以验证一个值是否属于特定数据类型?
- python - 在 pytest 中引入多个不同文件路径的 Pythonic 方法
- tensorflow2.0 - TensorFlow Extended:在 Schema 中指定特征的效价
- azure-devops-migration-tools - Azure DevOps 迁移器工具 - System.NullReferenceException:对象引用未设置为对象的实例
- java - 设置自动装配后运行 dataSource bean