【发布时间】:2019-02-06 08:50:56
【问题描述】:
我创建了一个网站,它在 Chrome 中的工作方式与它在 Chrome 中的工作方式完全相同,但在 IE11 中却无法正常工作。当按下按钮时,按钮上方应该会出现一个弹出窗口,其中包含来自 HTML 的文本(这在 Chrome 中完美运行)
在 IE11 上运行时出现此错误:
SCRIPT5007:无法获取未定义或 null 的属性“切换” 参考
script.js (3,3)
这指向我的 js 文件(称为 script.js):
function myFunction(data) {
var popup = document.getElementById("myPopup"+data);
popup.classList.toggle("show");
}
单击按钮查看弹出窗口时出现错误,仅当我按下按钮时才会调用该函数。
有没有办法让它在 IE11 和 Chrome 中工作?
请参阅下面的 sn-p,了解弹出窗口在 IE11 中的工作方式。
function myFunction(data) {
var popup = document.getElementById("myPopup" + data);
popup.classList.toggle("show");
}
/* Popup container */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
width: 100%;
padding-top: 15px;
padding-bottom: 15px;
background: white;
border: none;
font-size: 25px;
}
.popup:hover {
background-color: #008CBA;
color: white;
}
/* The actual popup (appears on top) */
.popup .popuptext {
visibility: hidden;
width: 250px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 110%;
left: 30%;
margin-left: -80px;
font-size: 16px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class when clicking on the popup container (hide and show the popup) */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
-moz-animation: fadeIn 1s;
-ms-animation: fadeIn 1s;
animation: fadeIn 1s;
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<table>
<tr>
<th colspan="4">Test Buttons</th>
</tr>
<tr>
<td><button class="popup" onclick="myFunction(1)">1<span class="popuptext" id="myPopup1">Test Button 1</span></button></td>
<td><button class="popup" onclick="myFunction(2)">2<span class="popuptext" id="myPopup2">Test Button 2</span></button></td>
<td><button class="popup" onclick="myFunction(3)">3<span class="popuptext" id="myPopup3">Test Button 3</span></button></td>
<td><button class="popup" onclick="myFunction(4)">4<span class="popuptext" id="myPopup4">Test Button 4</span></button></td>
</tr>
</table>
</body>
</html>
【问题讨论】:
-
你能发一个minimal reproducible example吗?我认为如果 MDN 的表格是正确的,IE10+ supported
classList(在有限的范围内) -
@CertainPerformance 添加
-
谢谢,虽然在我的 IE11 (11.253.17763.0) 上,我没有看到错误。该类看起来已正确添加,但动画/
.popuptext外观似乎没有发生,可能是由于 IE CSS 问题。 -
在版本 11.523.17134.0 上相同,没有错误和正确添加类
-
我使用的是版本 11.253.17763.0,我在控制台中看到了错误。环顾四周,我发现支持 classList 的次数越多 9+
标签: javascript internet-explorer