如何在IE9中使用垫片来处理classList错误?

时间:2017-09-02 05:28:23

标签: javascript html popup shim

我在按下图标时尝试弹出图像。图像有一个十字架,然后我用它来关闭图像。在ie10中这可以工作但是在ie9(和ie8)中我得到了classList的错误。我尝试了各种垫片,但错误仍然存​​在。不确定我是否正确使用垫片。

function myPopFunction(elem) {
var popup = document.getElementById(elem.id+"a");
popup.classList.toggle("showpop");
  

无法获得财产'切换'未定义或空引用

HTML是:

<div class="popup" onclick="myPopFunction(this)" id="myPop1">
<img src="Images/Slightly%20Smile.png" width="15">
<span class="popupimage" id="myPop1a">
    <div style="position: relative;">
        <img src="Images/dilbert_agile_programming.gif">
        <img src="Images/Cross%20hair%20white%20border%20small.png" class="dropshadow" width="25">
    </div>
</span>

CSS是:

.dropshadow {
border-radius:50%; 
box-shadow: 5px 5px 3px #888888;
position:absolute;
top:-10px; 
right:-10px;    
}

.popup {
position: relative;
display: inline-block;
cursor:pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.popup .popupimage {
visibility: hidden;
position: absolute;
z-index: 1;
top:100%;
left: -320px;  
}
.popup .showpop {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}

@-webkit-keyframes fadeIn {
from {opacity: 0;} to {opacity: 1;}
}
@keyframes fadeIn {
from {opacity: 0;} to {opacity:1 ;}

2 个答案:

答案 0 :(得分:0)

尝试使用此polyfill:

https://github.com/Financial-Times/polyfill-service/blob/master/polyfills/Element/prototype/classList/polyfill.js

这对IE8和IE9工作正常,但不适用于IE10和IE11。

enter image description here

答案 1 :(得分:0)

我设法让它在iE9中运行。将代码更改为:

function myPopFunction(elem) {
var popup = document.getElementById(elem.id+"a");
var cl=classList(popup);
cl.toggle("showpop");
}

我刚刚意识到我实际上也希望它能在IE7中运行。 classList.js shim我发布了一个上面的链接在ie7中不起作用。