function - 在IE中找不到切换LI元素的解决方案,其他浏览器都可以
问题描述
(function() {
window.onload = function() {
document.getElementById("checking").onclick = function() {
document.getElementById("knoz").style.display = !this.checked ? null : "none";
document.getElementById("acces").style.display = this.checked ? null : "none";
};
};
})();
.selector ul li {
list-style: none;
float: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<input type="checkbox" id="checking">check me
<div class="selector">
<ul>
<li id="knoz" name="knoz">element knoz</li>
<li id="acces" name="acces" style="display:none">element acces</li>
</ul>
</div>
当复选框 id=checking 被选中时,此函数会切换出 2 个 LI 元素。它可以在除 Internet Explorer 之外的所有其他浏览器中完美运行。
1 Li 元素有 style="display: none;" 另一个有 style="display: block;"
(function() {
window.onload = function() {
document.getElementById("checking").onclick = function() {
document.getElementById("knoz").style.display = !this.checked ? null : "none";
document.getElementById("acces").style.display = this.checked ? null : "none";
};
};
})();
对于所有浏览器,代码完美地切换了 2 个 LI 元素。在 IE 中,它隐藏了初始的 LI,但不显示替换的 LI。取消勾选复选框什么都不做。这两个元素都保持隐藏状态。
解决方案
尝试如下修改您的代码(将“null”更改为“block”):
<script>
(function () {
window.onload = function () {
document.getElementById("checking").onclick = function () {
document.getElementById("knoz").style.display = !this.checked ? "block" : "none";
document.getElementById("acces").style.display = this.checked ? "block" : "none";
};
};
})();
</script>
然后,在 IE 浏览器中的结果如下:
推荐阅读
- angular - 类型不可分配给类型 providedIn 缺少属性 ProvidedIn
- javafx-8 - 如何在 javafx 中保持媒体视图居中的同时拉伸媒体视图
- maven - 有没有办法将相同的 JavaFX 应用程序构建为具有不同构建类型的 .exe?
- json - 使用 bash 脚本从 json 计算累积时间
- c# - 这个功能不是生产质量,我怎样才能使它更健壮?
- java - 从java中的jsp请求参数而不使用servlet
- python - 如何在cython中的nogil循环下将numpy.ndarray分配给临时变量?
- excel - 如何在 VBA 中的函数中返回数组 - Excel
- powershell - 触发 Jenkins 作业的 PowerShell 命令引发身份验证错误
- postgresql - Postgres - 从数组到多行