【发布时间】:2019-08-03 14:28:41
【问题描述】:
我正在尝试在 js 中使用 onclick 代码和 style.display 来隐藏某物并进行潜水,默认情况下它的显示为 none 以获取阻止和出现。
第一个不会隐藏,另一个不会出现!
脚本文件运行良好,我在 js 中有其他东西运行良好。
function showDiv() {
document.getElementById('chatbutton').style.display = "none";
document.getElementById('chatbox').style.display = "block";
}
<a href="" id="chatbutton" >
<div class="mychat text-center" onclick="showDiv()">
<p class="chattext">Chat Support</p>
</div>
</a>
<div id="chatbox" class="mychat_open text-center d-none">
<p class="chattext">Chat Support</p>
<div class="form-group">
<input type="text" class="form-control" id="usr" placeholder="Enter your Name...">
<input type="text" class="form-control" id="usr" placeholder="Enter your Email...">
</div>
</div>
.mychat_open{
width: 15vw;
height: 20vh;
background-color: black;
position: fixed;
bottom: 20px;
right: 20px;
color: white;
opacity: 0.8;
min-height: 28px;
}
【问题讨论】:
-
您已将文本包装在
<a href="">中,这意味着单击将运行代码,然后导致页面重新加载。不要使用内联代码。改用这个:document.getElementById('chatbutton').onclick = function (e) { e.preventDefault(); /* rest of code */ };(并确保该脚本位于主要 HTML 内容下方) -
另外,带有空href的
<a href="">在不同的浏览器中做不同的事情。不要在生产中使用它。 -
哦,谢谢,从 中删除了 href,现在 在点击时被隐藏,但其他不会出现
标签: javascript html