< a onclick =" someFunction">有什么区别?和< a onclick =“someFunction()”> [英] what's the difference between <a onclick="someFunction"> and <a onclick="someFunction()">

查看:67
本文介绍了< a onclick =" someFunction">有什么区别?和< a onclick =“someFunction()”>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< a onclick =someFunction>

有什么区别?



< a onclick =someFunction()>



一个使用括号而不使用另一个,但使用哪一个有什么区别?什么是正确选项?如果我不使用任何 href 属性?



据我所知,在javascript中,使用 something = someFunc(); 将该函数的返回值赋给 something 变量。并使用 something = someFunc; 将函数直接(而不是其结果)赋值给该变量(它主要用于将函数赋值给事件)。例如我可以为一个onclick事件分配一个函数。



但是我不明白在某些html元素内联事件中使用时会发生什么,因为赋值不是指向一个javascript变量,而是指向一个html属性,这恰好是一个事件?请解释。

此外,将内联onclick函数分配给锚点( a )有什么区别?对其他元素(例如 span div label 等)?他们有相同的效果吗?

旁注:
我一直在阅读这里关于如何运行一个函数,当点击一个链接,我已经理解是应该不是完成内联,而是使用不显眼的JavaScript。 (我提到它是为了避免引发争论),但在我看到的例子中,他们没有提到我在内联中提到的两个选项的差异。



编辑:这个问题是由于这里给出了一个答案,它没有在事件的函数中使用括号,没有人提到括号是需要的,所以我认为它是有效的。但我不知道使用()或没有区别。

解决方案 div>


一个使用括号而不使用另一个,但使用哪一个的区别?


< a onclick =someFunction> 不会执行任何操作。圆括号导致一个函数被称为。

有一个只包含一个标识符(不管是函数名,变量还是其他)的语句都不会做任何事情(除非抛出一个引用错误不存在)。


如果我不使用任何href属性会发生什么?




然后我会问你为什么首先使用< a> 元素。


另外,将一个内联onclick函数赋值给一个锚(a)对其他元素(例如span div标签等) p>

只有它们不是(默认情况下)可聚焦元素(也不是没有href属性的元素),所以click事件不要通过选择元素并按回车来触发。如果你想要一个在JS触发时会做某些事情的元素,并且当JS不可用时你没有明智的回退,那么使用一个按钮。


what is the difference between

<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 about how to run a function when clicking on a link, and I already understood is that is should not be done "inline", but instead using unobtrusive javascript. (I mention it to avoid debate about that), but in the examples I've seen they don't mention the difference of both options I mention when doing it inline.

Edit: This question was made because here they gave an answer which doesn't use the parenthesis in the function for the event, and nobody mentioned the parenthesis were needed, so I assume it is valid. yet I don't know what is the difference of using () or not.

解决方案

One uses the parenthesis and not the other, but what are the differences of using either?

<a onclick="someFunction"> won't do anything. The parenthesis cause a function to be called.

Having a statement consisting of nothing but an identifier (be it a function name, variable, or whatever) won't do anything (except throw a reference error if the variable doesn't exist).

And what happens if i dont use any href attribute?

Then I'd question why you were using an <a> element in the first place.

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)?

Only that they aren't (by default) focusable elements (nor is an a element without an href attribute), so the click event couldn't be triggered by tabbing to the element and pressing enter. If you want an element that will do something with JS when triggered, and you don't have a sensible fallback for when JS isn't available, use a button.

这篇关于&lt; a onclick =&quot; someFunction&quot;&gt;有什么区别?和&lt; a onclick =“someFunction()”&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆