我有一个可以运行的脚本,但我不确定它是否正确。这里至少按钮可以工作并且不会首先抛出错误。报错
Error attaching event listener: ReferenceError: element is not defined
这是代码
<html>
<head>
<title>test function</title>
</head>
<body>
<input type="button" id="markerinfo"name="next" class="next" value="ALMOST DONE" />
<script>
document.getElementById("markerinfo").addEventListener("click", addEventListenerWithRetry);
function addEventListenerWithRetry(markerinfo,click, fillmark, retryInterval = 5000, maxRetries = 3){
let retries = 0;
function tryAttachListener() {
try {
element.addEventListener(click, fillmark);
console.log('Event listener attached successfully.');
} catch (error) {
console.error('Error attaching event listener:', error);
if (retries < maxRetries) {
retries++;
console.log(`Retrying in ${retryInterval / 1000} seconds...`);
setTimeout(tryAttachListener, retryInterval);
} else {
console.error('Max retries reached. Event listener not attached.');
}
}
}
tryAttachListener();
}
//document.getElementById("markerinfo").addEventListener("click", fillmark);
function fillmark() {
console.error('It working!');
}
</script>
</body>
</html>
但是,当我让它看起来不错时,它给出了这个错误
Uncaught SyntaxError: missing formal parameter
现在一切看起来都很好,我很困惑,这是怎么回事?任何人都可以帮助我知道它一定是一些简单的东西,我错过了。我认为这与侦听器的参数有关
<html>
<head>
<title>test function</title>
</head>
<body>
<input type="button" id="markerinfo"name="next" class="next" value="ALMOST DONE" />
<script>
document.getElementById("markerinfo").addEventListener("click", addEventListenerWithRetry);
function addEventListenerWithRetry('markerinfo','click', fillmark, retryInterval = 5000, maxRetries = 3){
let retries = 0;
function tryAttachListener() {
try {
element.addEventListener('click', fillmark);
console.log('Event listener attached successfully.');
} catch (error) {
console.error('Error attaching event listener:', error);
if (retries < maxRetries) {
retries++;
console.log(`Retrying in ${retryInterval / 1000} seconds...`);
setTimeout(tryAttachListener, retryInterval);
} else {
console.error('Max retries reached. Event listener not attached.');
}
}
}
tryAttachListener();
}
//document.getElementById("markerinfo").addEventListener("click", fillmark);
function fillmark() {
console.error('It working!');
}
</script>
</body>
</html>
现在我也尝试过这样,但得到了不同的错误,这看起来是最好的
Uncaught SyntaxError: unexpected token: '{
这是代码
<html>
<head>
<title>test function</title>
</head>
<body>
<input type="button" id="markerinfo"name="next" class="next" value="ALMOST DONE" />
<script>
document.getElementById('markerinfo').addEventListener('markerinfo','click', fillmark, retryInterval = 20000, maxRetries = 3){
let retries = 0;
function tryAttachListener() {
try {
element.addEventListener('click', fillmark);
console.log('Event listener attached successfully.');
} catch (error) {
console.error('Error attaching event listener:', error);
if (retries < maxRetries) {
retries++;
console.log(`Retrying in ${retryInterval / 1000} seconds...`);
setTimeout(tryAttachListener, retryInterval);
} else {
console.error('Max retries reached. Event listener not attached.');
}
}
}
tryAttachListener();
}
//document.getElementById("markerinfo").addEventListener("click", fillmark);
function fillmark() {
console.error('It working!');
}
</script>
</body>
</html>
ReferenceError: element is not defined
在
tryAttachListener
函数中,您尝试使用 element.addEventListener
而不定义 element
是什么。这会导致 ReferenceError
。
要解决此问题,请将
element
替换为实际的 DOM 元素,在本例中为 document.getElementById("markerinfo")
。
Uncaught SyntaxError: missing formal parameter
在此代码片段中,您在函数定义中使用了无效语法:
function addEventListenerWithRetry('markerinfo', 'click', fillmark, retryInterval = 5000, maxRetries = 3) { ... }
JavaScript 函数中的参数名称不能是字符串(例如,
'markerinfo'
、'click'
)。字符串不是有效的标识符。
使用有效的参数名称,例如
element
、eventType
和 callback
。
Uncaught SyntaxError: unexpected token: '{'
这部分代码:
document.getElementById('markerinfo').addEventListener('markerinfo','click', fillmark, retryInterval = 20000, maxRetries = 3){
不是有效的 JavaScript。您混淆了函数调用和函数定义:
addEventListener
不支持直接传递retryInterval
或maxRetries
等参数{
是无效语法,因为它不是函数或块的一部分。