首页 > 解决方案 > classList.toggle("show") 在 IE11 中不起作用

问题描述

我创建了一个网站,它在 Chrome 中的工作方式与它应该完全一样,但它在 IE11 中不起作用。当按下按钮时,按钮上方应该会出现一个弹出窗口,其中包含来自 HTML 的文本(这在 Chrome 中完美运行)

在 IE11 上运行时出现此错误:

SCRIPT5007:无法获取未定义或空引用的属性“切换”

脚本.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>

标签: javascriptinternet-explorer

解决方案


尝试使用以下代码进行测试可能会帮助您解决问题。

<!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 中的输出:

在此处输入图像描述

此外,您可以尝试根据您的要求修改代码。


推荐阅读