JavaScript Onclick事件解释

The onclick event in JavaScript lets you as a programmer execute a function when an element is clicked.

JavaScript中的onclick事件可让您作为程序员在单击元素时执行功能。

按钮Onclick示例 (Button Onclick Example)

<button onclick="myFunction()">Click me</button><script>function myFunction() {alert('Button was clicked!');}
</script>

In the simple example above, when a user clicks on the button they will see an alert in their browser showing Button was clicked!.

在上面的简单示例中,当用户单击按钮时,他们将在浏览器中看到一条警告,显示Button was clicked!

动态添加onclick (Adding onclick dynamically)

The onclick event can also be programmatically added to any element using the following code in the following example:

在以下示例中,还可以使用以下代码将onclick事件以编程方式添加到任何元素:

<p id="foo">click on this element.</p><script>var p = document.getElementById("foo"); // Find the paragraph element in the pagep.onclick = showAlert; // Add onclick function to elementfunction showAlert(event) {alert("onclick Event triggered!");}
</script>

注意 (Note)

It’s important to note that using onclick we can add just one listener function. If you want to add more, just use addEventListener(), which is the preferred way for adding events listener.

请务必注意,使用onclick只能添加一个侦听器功能。 如果要添加更多内容,只需使用addEventListener(),这是添加事件侦听器的首选方法。

In the above example, when a user clicks on the paragraph element in the html, they will see an alert showing onclick Event triggered.

在上面的示例中,当用户单击htmlparagraph元素时,他们将看到显示onclick Event triggered的警报。

防止默认动作 (Preventing default action)

However if we attach onclick to links (HTML’s a tag) we might want prevent default action to occur:

但是,如果我们将onclick附加到链接(HTML a标记),我们可能希望防止发生默认操作:

<a href="https://guide.freecodecamp.org" onclick="myAlert()">Guides</a><script>function myAlert(event) {event.preventDefault();alert("Link was clicked but page was not open");}
</script>

In the above example we prevented default behavior of a element (opening link) using event.preventDefault() inside our onclick callback function.

在上面的示例中,我们在onclick回调函数中使用event.preventDefault()防止a元素(打开链接)的默认行为。

MDN

MDN

其他资源 (Other Resources)

jQuery .on() Event Handler Attachment

jQuery .on()事件处理程序附件

翻译自: https://www.freecodecamp.org/news/javascript-onclick-event-explained/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390936.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

近似算法的近似率_选择最佳近似最近算法的数据科学家指南

近似算法的近似率by Braden Riggs and George Williams (gwilliamsgsitechnology.com)Braden Riggs和George Williams(gwilliamsgsitechnology.com) Whether you are new to the field of data science or a seasoned veteran, you have likely come into contact with the te…

VMware安装CentOS之二——最小化安装CentOS

1、上文已经创建了一个虚拟机&#xff0c;现在我们点击开启虚拟机。2、虚拟机进入到安装的界面&#xff0c;在这里我们选择第一行&#xff0c;安装或者升级系统。3、这里会提示要检查光盘&#xff0c;我们直接选择跳过。4、这里会提示我的硬件设备不被支持&#xff0c;点击OK&a…

什么是GraphQL? 普通神话被揭穿。

I love talking about GraphQL, especially with people who have been working with GraphQL or thinking of adopting GraphQL. One common question people have is why someone would want to move to GraphQL from REST. 我喜欢谈论GraphQL&#xff0c;特别是和那些一直在…

在Spring Boot里面,怎么获取定义在application.properties文件里的值

问题&#xff1a;在Spring Boot里面&#xff0c;怎么获取定义在application.properties文件里的值、 我想访问application.properties里面提供的值&#xff0c;像这样&#xff1a; logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR logging…

连接sqlexpress

sqlexpress在visualstudio安装时可选择安装。   数据源添加 localhost\sqlexpress window身份认证即可。转载于:https://www.cnblogs.com/zjxbetter/p/7767241.html

在Python中使用Seaborn和WordCloud可视化YouTube视频

I am an avid Youtube user and love watching videos on it in my free time. I decided to do some exploratory data analysis on the youtube videos streamed in the US. I found the dataset on the Kaggle on this link我是YouTube的狂热用户&#xff0c;喜欢在业余时间…

Win下更新pip出现OSError:[WinError17]与PerrmissionError:[WinError5]及解决

环境&#xff1a;Win7 64位&#xff0c;python3.6.0 我在准备用pip装东西的时候&#xff0c;在cmd里先更新了一下pip&#xff0c;大概是9.0.1更新到9.0. 尝试更新pip命令&#xff1a; pip install --upgrade pip 更新一半挂了 出现了 OSError:[WinError17] 与 PerrmissionError…

老生常谈:抽象工厂模式

在创建型模式中有一个模式是不得不学的,那就是抽象工厂模式(Abstract Factory),这是创建型模式中最为复杂,功能最强大的模式.它常与工厂方法组合来实现。平时我们在写一个组件的时候一般只针对一种语言,或者说是针对一个区域的人来实现。 例如:现有有一个新闻组件,在中国我们有…

ogc是一个非营利性组织_非营利组织的软件资源

ogc是一个非营利性组织Please note that freeCodeCamp is not partnered with, nor do we receive a referral fee from, any of the following providers. We simply want to help guide you toward a solution for your organization.请注意&#xff0c;freeCodeCamp不与以下…

数据结构入门最佳书籍_最佳数据科学书籍

数据结构入门最佳书籍Introduction介绍 I get asked a lot what resources I recommend for people who want to start their Data Science journey. This section enlists books I recommend you should read at least once in your life as a Data Scientist.我被很多人问到…

函数式编程概念

什么是函数式编程 简单地说&#xff0c;函数式编程通过使用函数&#xff0c;将值转换成抽象单元&#xff0c;接着用于构建软件系统。 面向对象VS函数式编程 面向对象编程 面向对象编程认为一切事物皆对象&#xff0c;将现实世界的事物抽象成对象&#xff0c;现实世界中的关系抽…

在Java里面怎么样在静态方法中调用getClass()?

问题&#xff1a;在Java里面怎么样在静态方法中调用getClass()&#xff1f; 我有一个类&#xff0c;它必须包含一些静态方法&#xff0c;在这些静态方法里面我需要像下面那样调用getClass() 方法 public static void startMusic() {URL songPath getClass().getClassLoader(…

变量名和变量地址

变量名和变量地址 研一时&#xff0c;很偶然的翻开谭浩强老先生的《C程序设计》&#xff08;是师姐的书&#xff0c;俺的老早就卖了&#xff0c;估计当时觉得这本书写得不够好&#xff09;&#xff0c;很偶然的看到关于变量名的一段话&#xff1a;“变量名实际上是一个符号地址…

多重插补 均值插补_Feature Engineering Part-1均值/中位数插补。

多重插补 均值插补Understanding the Mean /Median Imputation and Implementation using feature-engine….!了解使用特征引擎的均值/中位数插补和实现…。&#xff01; 均值或中位数插补&#xff1a; (Mean or Median Imputation:) The mean or median value should be calc…

域 嵌入图像显示不出来_如何(以及为什么)将域概念嵌入代码中

域 嵌入图像显示不出来Code should clearly reflect the problem it’s solving, and thus openly expose that problem’s domain. Embedding domain concepts in code requires thought and skill, and doesnt drop out automatically from TDD. However, it is a necessary …

linux 查看用户上次修改密码的日期

查看root用户密码上次修改的时间 方法一&#xff1a;查看日志文件&#xff1a; # cat /var/log/secure |grep password changed 方法二&#xff1a; # chage -l root-----Last password change : Feb 27, 2018 Password expires : never…

spring里面 @Controller和@RestController注解的区别

问题&#xff1a;spring里面 Controller和RestController注解的区别 spring里面 Controller和RestController注解的区别 Web MVC和REST applications都可以用Controller吗&#xff1f; 如果是的话&#xff0c;怎么样区别这个一个 Web MVC还是REST application呢 回答一 下面…

2流程控制

分支、循环 str1$1 str2$2 echo $# if [ $str1 $str2 ] thenecho "ab" elif [ "$str1" -lt "$str2" ] thenecho "a < b" elif [ "$str1" -gt "$str2" ] thenecho "a > b" elseecho "没有符…

客户行为模型 r语言建模_客户行为建模:汇总统计的问题

客户行为模型 r语言建模As a Data Scientist, I spend quite a bit of time thinking about Customer Lifetime Value (CLV) and how to model it. A strong CLV model is really a strong customer behavior model — the better you can predict next actions, the better yo…

linux bash命令_Ultimate Linux命令行指南-Full Bash教程

linux bash命令Welcome to our ultimate guide to the Linux Command Line. This tutorial will show you some of the key Linux command line technologies and introduce you to the Bash scripting language.欢迎使用我们的Linux命令行最终指南。 本教程将向您展示一些关键…