what is the difference between<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關於如何在點擊某個鏈接時運行某個功能的區別是什麼,我已經瞭解到應該是而不是可以「內聯」完成,而是使用不顯眼的javascript。 (我提到它是爲了避免對此發生爭論),但在我看到的例子中,他們沒有提到我在內聯中提到的兩個選項的區別。
編輯:這個問題是因爲here他們給出了一個答案,它沒有在事件的函數中使用括號,也沒有人提到括號是需要的,所以我認爲它是有效的。但我不知道使用()
與否有什麼不同。
'什麼是「正確的」選項?'既不使用['node.addEventListener'](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget。addEventListener) –