[英]I get an error when I try to output the function with the name `dinle()`. The error code is:Uncaught TypeError:
i have to anderstand whats rong with my function我必须了解我的 function 有什么问题
document.querySelector("#btnClear").addEventListener("click",dinle(event));
function dinle(event){
console.log("deneme");
event.preventDefault();
}
It seems like event is undefined in your code & or it doesn't have preventDefault Property.似乎事件在您的代码中未定义 & 或者它没有 preventDefault 属性。
If you wan to use inBuild Event in button, you don't need to pass it as args.Try instead..如果您想在按钮中使用 inBuild 事件,则无需将其作为 args 传递。改为尝试..
// event is removed in the dinle fn call
document.querySelector("#btnClear").addEventListener("click",dinle);
function dinle(event){
console.log("deneme");
event.preventDefault();
}
event.preventDefault() must be at first line of your function definition and event shall not be the attribute in callback, do the following event.preventDefault() 必须在您的 function 定义的第一行,并且 event 不应是回调中的属性,请执行以下操作
document.querySelector("#btn").addEventListener("click",dinle); function dinle(event){ event.preventDefault(); console.log("deneme"); }
<button id="btn">CLICK here to run the function </button>
The addEventListener()
function takes a type and a listener as parameters. addEventListener()
function 将类型和侦听器作为参数。 where listener can be a callback function which gets called when the specified type of the event occurs.其中listener可以是一个回调 function 当指定类型的事件发生时被调用。
see documentation见文档
It seems like you are passing the result of your dinle(event)
which would be undefined since it does not return anything.似乎您正在传递您的
dinle(event)
的结果,该结果将是未定义的,因为它不返回任何内容。
What is actually causing the Uncaught TypeError
is the event parameter, because it cannot be resolved.实际上导致
Uncaught TypeError
的是事件参数,因为它无法解析。
Try passing the function itself as the parameter: addEventListener("click", dinle)
尝试将 function 本身作为参数传递:
addEventListener("click", dinle)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.