javascript - 脚本不记得将我的 div 设置为不显示
问题描述
我有一个订阅框,我正在尝试制作一个 X 关闭按钮,该按钮记住下次使用网络存储或 cookie 时不会显示。当我单击关闭按钮时,我的整个侧边栏设置为不显示。而不仅仅是 id="gfxhsub" 。
JavaScript
window.onload = function(){
document.getElementById('mailclose').onclick = function(){
this.parentNode.parentNode.parentNode
.removeChild(this.parentNode.parentNode);
return false;
};
};
html
<div id="gfxhsub" style="margin: 2em 0.5em; height: auto;">
<span id='mailclose'><i class="fa fa-times"></i></span>
[newsletter_signup_form id=6]
</div>
解决方案
在 css 中创建一个类 .d-none 并使用document.getElementById('gfxhsub').classList += "d-none"
而不是this.parentNode.parentNode.parentNode
.removeChild(this.parentNode.parentNode);
.d-none {
display: none
}
<div id="gfxhsub" style="margin: 2em 0.5em; height: auto;">
<span id='mailclose'><i class="fa fa-times"></i>X</span> [newsletter_signup_form id=6]
</div>
<script type="text/javascript">
window.onload = function() {
document.getElementById('mailclose').onclick = function() {
document.getElementById('gfxhsub').classList += "d-none"
return false;
};
};
</script>
推荐阅读
- r - 通过 R 中的 LM 模型的变量列表创建循环
- javascript - Redux thunk 影响不同的 reducer
- java - http java调用出错了
- javascript - 在 react-native 中使用 axios 生成访问令牌
- php - 想要同时提取
从以下 XML 响应中使用 PHP - javascript - 如何为简单的 crud 应用程序创建编辑功能?- 使用 VANILLA JAVASCRIPT 进行 CRUD 操作
- refresh - 在 Raspberry Pi 上刷新铬的问题
- postgresql - 使用Kafka的两种方式数据库同步
- python-3.x - 尽管对象的定义具有列表内容,但作为实例属性的列表仍为空
- react-native - 播放视频时 react-native-track-player 不会停止