我创建了一个网站,该网站在Chrome浏览器中的运作方式完全相同,但在IE11中却无法运行。按下按钮后,该按钮上方应显示一个弹出窗口,其中包含HTML文本(在Chrome中效果很好)
在IE11上运行时,出现此错误:
SCRIPT5007:无法获取未定义或空值的属性“ toggle” 参考
script.js(3,3)
这指向我的js文件(称为script.js):
function myFunction(data) {
var popup = document.getElementById("myPopup"+data);
popup.classList.toggle("show");
}
单击按钮查看弹出窗口时出现错误,仅当按下按钮时才会调用该函数。
是否有办法使它在IE11和Chrome中都能正常工作?
有关在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>
答案 0 :(得分:0)
尝试使用下面的代码进行测试可能会帮助您解决问题。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* Popup container - can be anything you want */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* The actual popup */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}
/* 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 - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-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 ;}
}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h2>Test</h2>
<table>
<tr>
<td>
<div class="popup" onclick="myFunction(1)"><h2>1</h2>
<span class="popuptext" id="myPopup1">Popup 1</span>
</div>
</td>
<td>
<div class="popup" onclick="myFunction(2)"><h2>2</h2>
<span class="popuptext" id="myPopup2">Popup 2</span>
</div>
</td>
<td>
<div class="popup" onclick="myFunction(3)"><h2>3</h2>
<span class="popuptext" id="myPopup3">Popup 3</span>
</div>
</td>
<td>
<div class="popup" onclick="myFunction(4)"><h2>4</h2>
<span class="popuptext" id="myPopup4">Popup 4</span>
</div>
</td>
</tr>
</table>
<script>
function myFunction(data) {
var txt= "myPopup" + data;
var popup = document.getElementById(txt);
popup.classList.toggle("show");
}
</script>
</body>
</html>
IE 11中的输出:
此外,您可以尝试根据需要修改代码。