javascript - addEventListener 不适用于默认参数
问题描述
如果我用 调用函数,为什么具有默认参数的函数不起作用addEventListener
?
这是一个例子:
下面这个示例函数会生成一些随机字符串,我通过onclick()
在 html 文件中使用来调用该函数,它完美地工作
const notationGraph = {
"U": ["L", "R", "F", "B"],
"D": ["L", "R", "F", "B"],
"L": ["U", "D", "F", "B"],
"R": ["U", "D", "F", "B"],
"F": ["U", "D", "L", "R"],
"B": ["U", "D", "L", "R"],
};
const switches = ["", "\'", "2"];
function randItem(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function generate3x3(Min = 20, Max = 25) {
let scrambles = [];
let prev;
const len = Math.floor(Math.random() * (Max - Min + 1) + Min);
for (let i=0; i<len; i++) {
let notation = randItem(prev ? notationGraph[prev] : Object.keys(notationGraph));
let swtch = randItem(switches);
scrambles.push(`${notation}${swtch}`);
prev = notation;
}
console.log(scrambles.join(" "));
}
<button id="buttonTest" onclick="generate3x3()">test button</button>
但是现在,如果我尝试该addEventListener
方法而不是onclick()
像下面的代码那样,它不起作用并且console.log
是空字符串
const notationGraph = {
"U": ["L", "R", "F", "B"],
"D": ["L", "R", "F", "B"],
"L": ["U", "D", "F", "B"],
"R": ["U", "D", "F", "B"],
"F": ["U", "D", "L", "R"],
"B": ["U", "D", "L", "R"],
};
const switches = ["", "\'", "2"];
function randItem(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function generate3x3(Min = 20, Max = 25) {
let scrambles = [];
let prev;
const len = Math.floor(Math.random() * (Max - Min + 1) + Min);
for (let i=0; i<len; i++) {
let notation = randItem(prev ? notationGraph[prev] : Object.keys(notationGraph));
let swtch = randItem(switches);
scrambles.push(`${notation}${swtch}`);
prev = notation;
}
console.log(scrambles.join(" "));
}
document.getElementById("buttonTest").addEventListener("click", generate3x3)
<button id="buttonTest">test button</button>
我想知道为什么它不起作用,我做错了什么?
解决方案
因为在 eventListener 中传递的事件正在覆盖你的第一个默认参数
要么将事件作为第一个参数添加到函数中
function generate3x3(event, Min = 20, Max = 25) {
或者这样称呼它
document.getElementById("buttonTest").addEventListener("click",function() {
generate3x3();
})
const notationGraph = {
"U": ["L", "R", "F", "B"],
"D": ["L", "R", "F", "B"],
"L": ["U", "D", "F", "B"],
"R": ["U", "D", "F", "B"],
"F": ["U", "D", "L", "R"],
"B": ["U", "D", "L", "R"],
};
const switches = ["", "\'", "2"];
function randItem(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function generate3x3(Min = 20, Max = 25) {
let scrambles = [];
let prev;
const len = Math.floor(Math.random() * (Max - Min + 1) + Min);
for (let i=0; i<len; i++) {
let notation = randItem(prev ? notationGraph[prev] : Object.keys(notationGraph));
let swtch = randItem(switches);
scrambles.push(`${notation}${swtch}`);
prev = notation;
}
console.log(scrambles.join(" "));
}
document.getElementById("buttonTest").addEventListener("click",function() {
generate3x3()
})
<button id="buttonTest">test button</button>
推荐阅读
- java - 在 UIMA 框架中使用 Stanford coreNlp
- google-bigquery - Bigquery 中的部署自动化
- r - r 带有 containerRit 的 Dockerfile 导致关闭错误
- c# - asp.net ActionResult 看不到控制器中的静态数据
- batch-file - 获取压缩档案名称的一部分并将其附加到其中的文件中
- ios - 304 在 iOS Cordova 应用上播放 html5 视频时未修改
- javascript - 如何从图像中删除标题
- r - R data.table 根据 j 中的列动态过滤行
- android - proguard 不删除日志
- node.js - 如何在打字稿中使用节点配置?