Javascript jQuery单击事件在IE11 Windows8中不起作用

Javascript jQuery单击事件在IE11 Windows8中不起作用,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,不确定为什么这样做不起作用,但由于某种原因,click事件正在触发一个控制台.log,但是它没有对我试图针对的HTML元素执行addClass。不确定是否有什么我需要注意的。我已经在我的Mac电脑上用Chrome进行了测试,效果很好,但是当我在运行Windows8的桌面上进行测试时,没有任何效果 以下是我所拥有的: HTML: <div class="window-element"> <p>thing to test</p> <a href="#" cl

不确定为什么这样做不起作用,但由于某种原因,click事件正在触发一个
控制台.log
,但是它没有对我试图针对的HTML元素执行
addClass
。不确定是否有什么我需要注意的。我已经在我的Mac电脑上用Chrome进行了测试,效果很好,但是当我在运行Windows8的桌面上进行测试时,没有任何效果

以下是我所拥有的:

HTML:

<div class="window-element">
<p>thing to test</p>
<a href="#" class="close-box">Close this</a>
</div>
$(".close-box").on('click', function(e){
    $(".window-element").addClass("hidden");
    console.log("click test");
    e.preventDefault();
});
<div class="window-element">
<p>thing to test</p>
<a href="#" class="close-box">Close this</a>
</div>
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();
});

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

我建议您对以下特定代码段使用香草JavaScript

HTML:

<div class="window-element">
<p>thing to test</p>
<a href="#" class="close-box">Close this</a>
</div>
$(".close-box").on('click', function(e){
    $(".window-element").addClass("hidden");
    console.log("click test");
    e.preventDefault();
});
<div class="window-element">
<p>thing to test</p>
<a href="#" class="close-box">Close this</a>
</div>
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();
});

选中此

以尽可能少地进行更改。我会将您的JS更改为:

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

这似乎对我有用。试试这个JSFIDLE,看看这是否是你想要的:

“我建议你使用香草JavaScript”
-为什么?@user7290573 IE11&
事件监听器似乎有任何问题。我在网上查了一些训练&似乎用香草JavaScript就能解决这个问题。检查。让我知道它是否有效……jQuery是专门为跨平台兼容而设计的。事件处理程序不是我想出来的问题。看起来另一个脚本(WordPress插件)出现了问题,它抛出了错误并杀死了IE11页面上的其余脚本。启动插件,砰的一声,东西又开始工作了。我想指出的是,走香草JS路线似乎是“更安全”的赌注,但我最初的jQuery也同样有效。谢谢@RoryMcCrossan您是对的,但这也取决于OP使用的jQuery版本。有时,根据jQuery版本或浏览器的不同,会出现bug。例如,这是我在jQuery的最新版本中发现的一个问题,这个问题已经解决了。这与此问题无关,但它表明有时存在浏览器不一致。这就是为什么我建议用vanilla js来解决这个问题。