如何使一个JavaScript函数控制两个HTML ID?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使一个JavaScript函数控制两个HTML ID?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1896字,纯文字阅读大概需要3分钟。
内容图文
我有这个按钮标签元素
<button class="btn btn-link" type="button" onclick="contentDownloads()">
<img src="./downloads/content-textures.svg" width="120px" />
</button>
并具有javascript功能
function contentDownloads() {
var x = document.getElementById("vipDownloads");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
但是,将来我会包括更多按钮,并实现相同的功能.
这是包含ID的部分:
<div id="vipDownloads" class="collapse show" style="display: none;">
<div class="card-body">
<h5 class="card-title">Map Pack v 1.8 <span class="caption-entry">by cWaLker</span> </h5>
<p class="card-text">
Aight Fellas,
Map Pack v 1.8 introduces some revived classics and under the radar stunners. <br>
<br> 1.7 went on a diet and dropped some restraining pounds.
For this nugget to work stable, you'd need to remove your own user folder first and then drop the User folder from the 1.8 bulk package.. it will serve you everything you need anyways ;-)<br>
<br> Have Fun Guys lets kick some flips!
</p>
<a href="https://drive.google.com/open?id=1SNivhvHi-4PjPDy1tob-91HYs3x3R_z9" target="_blank"><button type="button" class="btn btn-outline-success">Download</button></a><br>
</div>
</div>
您可以在此处看到我如何设计按钮和内容http://thpsblog.000webhostapp.com/downloads.html(主机上的CSS可能需要一段时间才能真正更新,可能包括一些白色的白色)
该功能隐藏和取消隐藏内容.
我已经找到了一些解决方案,但是它们都是在jQuery中键入的,不幸的是,我不知道jQuery.
如何使此函数带有两个唯一的ID?
解决方法:
设置元素的ID以切换函数的参数,并在调用函数时传递它.
function contentDownloads(id) {
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
<button class="btn btn-link" type="button" onclick="contentDownloads('vipDownloads')">
<img src="./downloads/content-textures.svg" width="120px" />
</button>
内容总结
以上是互联网集市为您收集整理的如何使一个JavaScript函数控制两个HTML ID?全部内容,希望文章能够帮你解决如何使一个JavaScript函数控制两个HTML ID?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。