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.pswp.cn/news/390936.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390936.shtml
英文地址,請注明出處:http://en.pswp.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命令行最終指南。 本教程將向您展示一些關鍵…