what is the difference betweensự khác biệt giữa <a onclick="someFunction"> and <a onclick="someFunction()">
<a onclick="someFunction">
and
<a onclick="someFunction()">
One uses the parenthesis and not the other, but what are the differences of using either? What is the "correct" option? And what happens if i dont use any href
attribute?
As far as I know, in javascript, using something = someFunc();
assigns the return value of that function to the something
variable. And using something = someFunc;
assigns the function directly (not its result) to that variable (And it's mostly used to assign functions to events). e.g. I can assign a function to a onclick event.
But what I don't understand is what happens when using either in some html element inline event, as in the examples, since the assignation is not to a javascript variable, but to an html attribute, which happens to be an event? Please explain.
And also, is there a difference on assigning a inline onclick function to an anchor (a
) that to other elements (e.g. span div label
etc)? Do they have the same effect?
Sidenote: I've been reading here về cách chạy một hàm khi nhấp vào liên kết và tôi đã hiểu là không phải là được thực hiện "nội tuyến", mà thay vào đó là sử dụng javascript không phô trương. (Tôi đề cập đến nó để tránh tranh luận về điều đó), nhưng trong các ví dụ tôi đã thấy họ không đề cập đến sự khác biệt của cả hai tùy chọn tôi đề cập đến khi làm nó nội tuyến.
Chỉnh sửa: Câu hỏi này được thực hiện vì here họ đưa ra câu trả lời không sử dụng dấu ngoặc đơn trong hàm cho sự kiện và không ai đề cập đến dấu ngoặc đơn, vì vậy tôi cho rằng nó hợp lệ. nhưng tôi không biết sự khác biệt của việc sử dụng ()
hay không.
'Tùy chọn" chính xác "là gì?' Không, sử dụng ['node.addEventListener'] (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener) –