gpt4 book ai didi

javascript - 引用错误 - Mozilla Firefox

转载 作者:行者123 更新时间:2023-12-03 02:32:23 26 4
gpt4 key购买 nike

我正在研究移动过滤器。有几个按钮。单击每个按钮后接收类active-filter-btns。我做了一切,但在 Firefox 测试期间,我收到错误 ReferenceError: event is not Defined。 Chrome 和 Safari 工作正常。对于 Mozilla 看到错误的原因以及如何修复它有什么想法吗?或者也许有不同的方式来添加事件类

HTML:

 <div id="search-btns" class="filter-btns">
<button id="filter-btn-all" class="filter-btn" onclick="Project.Search.selectCategory(this.id)">A - Z</button>
<button id="filter-btn-provider" class="filter-btn" onclick="Project.Search.selectCategory(this.id)">Providers</button>
<button id="filter-btn-jackpot" class="filter-btn" onclick="Project.Search.selectCategory(this.id)">Jackpot</button>
</div>

JS:

Project.Search.selectCategory = function(event) {
if(event.target.classList.contains('active-filter-btns')){
this.showCategories();
} else {
switch(e) {
case "filter-btn-all":
this.showAllGames();
break;
case "filter-btn-provider":
this.showProviders();
break;
case "filter-btn-jackpot":
this.showJackpotGames();
break;
}
}

if (!event.target.classList.contains('filter-btn'))
return;

event.target.classList.toggle('active-filter-btns');

let links = document.querySelectorAll('.filter-btn');
for (let i = 0; i < links.length; i++) {
if (links[i] === event.target)
continue;
links[i].classList.remove('active-filter-btns');
}
};

最佳答案

试试这个,在 Firefox 中,你必须传递事件对象。

Project.Search.selectCategory = function(event) {
if(event.target.classList.contains('active-filter-btns')){
this.showCategories();
} else {
switch(event.target.id) {
case "filter-btn-all":
this.showAllGames();
break;
case "filter-btn-provider":
this.showProviders();
break;
case "filter-btn-jackpot":
this.showJackpotGames();
break;
}
}

if (!event.target.classList.contains('filter-btn')) {
return;
}

event.target.classList.toggle('active-filter-btns');

let links = document.querySelectorAll('.filter-btn');
for (let i = 0; i < links.length; i++) {
if (links[i] === event.target) {
continue;
}
links[i].classList.remove('active-filter-btns');
}
};
<div id="search-btns" class="filter-btns">
<button id="filter-btn-all" class="filter-btn" onclick="Project.Search.selectCategory(event)">A - Z</button>
<button id="filter-btn-provider" class="filter-btn" onclick="Project.Search.selectCategory(event)">Providers</button>
<button id="filter-btn-jackpot" class="filter-btn" onclick="Project.Search.selectCategory(event)">Jackpot</button>
</div>

这个演示可以说明原因:您必须在 onclick 中将事件作为参数传递

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title>

</head>

<body>
<span id="counter" onclick="test(this.id,event)">1,234,567.15</span>
</body>
<script>
function test(event) {
console.log(arguments[0]);
console.log(arguments[1]);
}
</script>

</html>

如果没有通过,它将是未定义的,如下所示:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title><!--
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> -->
</head>

<body>
<span id="counter" onclick="test(this.id)">1,234,567.15</span>
</body>
<script>
function test(event) {
console.log(arguments[0]);
console.log(event);//counter,because you pass id 'counter' ,you didn't pass event
}
</script>

</html>

关于javascript - 引用错误 - Mozilla Firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com