onclick(HTML属性)

描述 (Description)

onclick event handler captures a click event from onclick事件处理程序从中捕获click事件
the users’ mouse button on the element to which the
用户的鼠标按钮 onclick attribute is applied. This action usually onclick属性。 这个动作通常
results in a call to a script method such as a JavaScript function, like
导致对脚本方法(例如JavaScript函数)的调用,例如
this:
这个:
onclick="displayHelpInfo();"

However, it can also be used to run a script in situ:

但是,它也可以用于原位运行脚本:

onclick="alert('You are clicking on me');"

Note that this event attribute cannot be applied to the following elements:

请注意,此事件属性不能应用于以下元素:

  • applet

    applet

  • base

    base

  • basefont

    basefont

  • bdo

    bdo

  • br

    br

  • font

    font

  • frame

    frame

  • frameset

    frameset

  • head

    head

  • html

    html

  • iframe

    iframe

  • isindex

    isindex

  • meta

    meta

  • param

    param

  • script

    script

  • style

    style

  • title

    title

(Example)

Clicking anywhere on the div below will call a function, defined elsewhere, called showStats():

单击下面div上的任意位置将调用在其他地方定义的函数showStats()

<div onclick="showStats();">Figures for February’s racing.</div>

(Value)

This attribute has no fixed value. It’s up to the author to decide on the scripting that’s included here, be that a call to one or more defined functions, or a simple alert() statement.

此属性没有固定值。 由作者决定此处包含的脚本,是对一个或多个定义函数的调用,还是简单的alert()语句。

However, the likely values will be similar to this:

但是,可能的值将与此类似:

onclick="doMyFunction();"

You could also specify a value like this:

您还可以指定一个像这样的值:

onclick="doThisFunction();thenDoTheOtherFunction();"

You may also use a value like this:

您还可以使用如下所示的值:

onclick="alert('Hello world');window.close();"

Note that you can string several functions together, separating them with a semicolon, as shown in the second and third examples above.

请注意,您可以将多个函数串在一起,并用分号隔开,如上面的第二个和第三个示例所示。

兼容性 (Compatibility)

Internet ExplorerFirefoxSafariOpera
5.56.07.01.01.52.01.32.03.09.29.5
FullFullFullFullFullFullFullFullFullFullFull
IE浏览器 火狐浏览器 苹果浏览器 歌剧
5.5 6.0 7.0 1.0 1.5 2.0 1.3 2.0 3.0 9.2 9.5
充分 充分 充分 充分 充分 充分 充分 充分 充分 充分 充分

Every browser listed supports this attribute. However, inline event handlers such as this should be avoided. In the same way that inline CSS styles are frowned upon but externally defined CSS styles are considered good practice, inline event handlers should be stripped out and replaced with events attached unobtrusively through the DOM.

列出的每个浏览器都支持此属性。 但是,应避免使用此类内联事件处理程序。 与不赞成使用内联CSS样式,但将外部定义CSS样式视为一种好习惯一样,应删除内联事件处理程序,并用通过DOM附加地添加事件来代替。

翻译自: https://www.sitepoint.com/onclick-html-attribute/

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值