jQuery 点击事件在 IE11 中不工作 Windows8

jQuery click event not working in IE11 Windows8

不知道为什么这行不通,但出于某种原因,点击事件触发了 console.log 但随后它没有对 HTML 元素执行 addClass I '试图瞄准。不确定是否有任何需要注意的地方。我已经在我的 Mac 上的 Chrome 中测试了它并且它工作正常,但是当我在 Surface 运行 Windows8 上测试时,没有骰子。

这是我拥有的:

HTML:

<div class="window-element">
<p>thing to test</p>
<a href="#" class="close-box">Close this</a>
</div>

JS:

$(".close-box").on('click', function(e){
    $(".window-element").addClass("hidden");
    console.log("click test");
    e.preventDefault();
});

不确定是否还有其他需要解决的问题?

我建议您使用 vanilla JavaScript 作为这个特定的代码片段,如下所示:

HTML:

<div class="window-element">
<p>thing to test</p>
<a href="#" class="close-box">Close this</a>
</div>

JavaScript:

const closeBox = document.querySelector('.close-box');
const windowElement = document.querySelector('.window-element');

closeBox.addEventListener('click', function(e){
    windowElement.classList.add("hidden");
    console.log("click test");
    e.preventDefault();
});

检查这个working code sample

为了尽可能减少更改,我会将您的 JS 更改为:

$(document).on('click','.close-box', function(e){
    $('.window-element').addClass('hidden');
    console.log("click test");
    e.preventDefault();
});

这似乎对我有用。试试这个 jsfiddle 看看这是不是你要找的:https://jsfiddle.net/91myczwj/1/