搜尋
首頁web前端html教學HTML 事件屬性

HTML 事件屬性

Sep 04, 2024 pm 04:17 PM
htmlhtml5HTML TutorialHTML PropertiesHTML tags

在本文中,我們將詳細討論 HTML 事件屬性。事件是由於使用者操作而執行的操作。例如,當使用者按下鍵盤讀取資料時,就稱為鍵盤事件。當使用者查看網站並點擊按鈕或按下刷新按鈕加載頁面(其中瀏覽器對頁面進行操作)時,就會完成這些活動;所有這些動作都稱為一個事件。在這裡,我們將基本了解事件以及它如何在瀏覽器中處理使用者操作。整個瀏覽器視窗中會發生不同類型的事件,以下部分將對此進行說明。

前 5 個 HTML 事件屬性

HTML 事件屬性

HTML 中提供了不同的事件變體。所有這些事件都有一個名為事件處理程序的小程式碼區塊,該程式碼區塊在執行事件操作時觸發。它們附加到 HTML 元素。事件處理程序或事件偵聽器在 HTML 事件屬性中扮演重要角色。讓我們看看全域聲明並應用於 HTML 元素的不同類型的事件屬性以及它們的詳細工作。主要使用四個主要事件屬性。他們是:

  1. 視窗事件
  2. 表單事件
  3. 老鼠事件
  4. 鍵盤事件
  5. 拖放事件

我們將透過範例一一描述所有這些屬性。首先,我們一起去。

1.視窗事件

  • onafterprintEvent: 所有 Html 標籤都支援此屬性,並且在頁面開始列印並且具有單值腳本時起作用。這是 HTML 程式碼的範例。此範例顯示了按下按鈕時的情況;它會列印一條在對話方塊訊息中列印的訊息。

代碼:



<title>
Windows onafterprint Event
</title>


<h1 id="Windows-onafterprint-Event">Windows onafterprint Event </h1>
<p>This attribute works in IE and Mozilla</p>

<script>
function myfun() {
alert("Document is being printed");
}
</script>

輸出:

HTML 事件屬性

  • onbeforeprint:它在列印之前起作用。該事件在列印過程後被觸發。以下是範例程式碼。

代碼:



<style>
body {
color: #9370DB;
background-color: #BC8F8F;
text-align: center;
padding: 20px;
}
p {
font-size: 2rem;
}
</style>


<h1 id="Attribute-Demo"> Attribute Demo</h1>
<p style="color:#0000FF;">trigger to print.</p>
<div class="show"></div>
<script>
function get() {
document.body.style.background = "#00BFFF";
}
</script>

輸出:

HTML 事件屬性

  • onerror: 當沒有元素存在時拋出錯誤時會觸發此函數。

代碼:



<img  src="/static/imghwm/default1.png" data-src="p.jpg" class="lazy" onerror="myFun()" alt="HTML 事件屬性" >
<p>hello world.</p>
<script>
function myFun() {
alert("problem with image loading.");
}
</script>

輸出:

HTML 事件屬性

  • onload:此函數有助於載入對象,並且可以很好地查看網頁是否正確載入。

代碼:



<title>onload event demo</title>


<img  src="/static/imghwm/default1.png" data-src="pic.jpg" class="lazy" onload="ldImg()"    style="max-width:90%"  style="max-width:90%" alt="HTML 事件屬性" >
<script>
function ldImg() {
alert("image loaded without error");
}
</script>

輸出:

HTML 事件屬性

  • onresize:當瀏覽器視窗大小調整時觸發該事件,任何元素在resize屬性下都可以觸發。

代碼:


<title>onresize event</title>


<script type="text/javascript">
function cmg() {
alert('welcome to educba');
}
window.onresize = cmg;
</script>


<input type="button" value="Click the button" onclick="alert(window.onresize);">

輸出:

HTML 事件屬性

  • onunload:當網頁視窗關閉或使用者離開網頁時觸發此事件。下面的程式碼會在用戶離開時卸載頁面並引發感謝搜尋的警報。此事件有時適用於所有瀏覽器。

代碼:


<h1 id="Welcometo-educba-tutorial">Welcometo educba tutorial</h1>
<p>Leave the page .</p>
<script>
function onfunc() {
alert("Thank you for searching!");
}
</script>

輸出:

HTML 事件屬性

2.表單事件

它與表單控制項一起使用。以下是使用者與瀏覽器互動時發生的屬性。

  • onblur:當使用者的注意力離開表單視窗時發生此事件。以下範例採用小寫形式輸入,按一下提交按鈕時,會將輸出轉換為大寫形式。

代碼:



<title> Form onblur </title>
<style>
body {
text-align:center;
}
h1 {
color:pink;
}
</style>


<h1 id="EDUCBA">EDUCBA</h1>
<input type="text" name="fname" id="aaa" onblur="myfunc()">
<button type="button">Submit</button>
<script>
function myfunc() {
var a = document.getElementById("aaa");
a.value = a.value.toUpperCase();
}
</script>

輸出:

HTML 事件屬性

HTML 事件屬性

  • onchange: 當使用者變更表單中的現有元素時,會發生此事件。當元素失去焦點時就會發生這種情況。

代碼:



<meta charset="utf-8">
<title>HTML onchange</title>


 

Note: Select any option

  

輸出:

HTML 事件屬性

  • onfocus: 當使用者專注於網頁上的元素或輸入獲得焦點時,會啟用此屬性。下面的範例突出顯示了我們在欄位中輸入的內容。

代碼:



<p>This event gets triggered whwn an element is been focussed.</p>
Name: <input type="text" id="name" onfocus="onfoc(this.id)"><br>
Location: <input type="text" id="loc" onfocus="onfoc(this.id)">
<script>
function onfoc(a) {
document.getElementById(a).style.background = "pink";
}
</script>

輸出:

HTML 事件屬性

  • oninput: This event triggers when the input is entered in the input field. It has been activated when the value in the text field is changed. It reflects once the value of the element is changed.


<title> HTML oninput </title>

<style>
body {
text-align:center;
}
h1 {
color:red;
}
</style>

<h1 id="Event-Attribute"> Event Attribute </h1>
Enter the text:
<input type="text" id="EDUCBA" oninput="myon()">
<p id="sid"></p>
<script>
function myon()
{
var x = document.getElementById("EDUCBA").value;
document.getElementById("sid").innerHTML = "Enter the text : " +x;
}
</script>

Output:

HTML 事件屬性

HTML 事件屬性

  • oninvalid: This attribute calls the events when the text entered in the input type is invalid or remains empty. This event attribute must fill the input element.

Code:



<title> example oninvalid Event </title>
<style>
p {
color:orange;
}
body {
text-align:center;
}
</style>     

<p> HTML is used to create a web page</p>
Enter the name:
 

Output:

HTML 事件屬性

  • onreset: It is fired when a form is a rest. The following example says when you submit the button, a form is processed, and again when you click to reset, the form is being reset.

Code:


<style>
body {font-family:calibri;}
label {font-variant:small-caps;}
ol {background-color:#610000; margin-top:35px;}
li {margin-top:3px; padding:3px; background-color:rose; font-size:15px;}
div {position:absolute;top:250px;left:70px; font-size:1.5em;
}
</style>

<ol>
<li>Form with input to reset and submit</li>
</ol>


<script> function onInva() { alert("Input field cannot be empty!"); } function onInp() { var input_value = document.getElementById("iv").value; document.getElementById("a_box").innerHTML = "Input value: <br />" + iv; } function onRes() { alert("form is reset!"); } function onSubmitEvent() { alert("Form is loading"); location.reload(); } </script>

Output:

HTML 事件屬性

  • onsearch: It works when a user presses an enter button.

Code:



<p>Write in the field.</p>&gt
<input type="search" id="value1" onsearch="myF()">
<p id="sample"></p>
<script>
function myF() {
var k = document.getElementById("value1");
document.getElementById("sample").innerHTML = "search element is: " + k.value;
}
</script>

Output:

HTML 事件屬性

  • onselect: It is triggered when a text has been selected in an input box. It throws a dialog box printing an alert message.

Code:



<title>onselect demo</title>
<style>
h1 {
color:magenta;
}
body {
text-align:center;
}
</style>
<script>
function eduhtml() {
alert("text highlighted!");
}
</script>


<h1 id="EDUCBA-Online-tutorial">EDUCBA Online tutorial</h1>
Text Box: <input type="text" value="onselectattribute: A well defined portal" onselect="eduhtml()">


以上是HTML 事件屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
為什麼HTML屬性對Web開發很重要?為什麼HTML屬性對Web開發很重要?May 12, 2025 am 12:01 AM

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外觀和功能

Alt屬性的目的是什麼?為什麼重要?Alt屬性的目的是什麼?為什麼重要?May 11, 2025 am 12:01 AM

alt屬性是HTML中標籤的重要部分,用於提供圖片的替代文本。 1.當圖片無法加載時,alt屬性中的文本會顯示,提升用戶體驗。 2.屏幕閱讀器使用alt屬性幫助視障用戶理解圖片內容。 3.搜索引擎索引alt屬性中的文本,提高網頁的SEO排名。

HTML,CSS和JavaScript:示例和實際應用HTML,CSS和JavaScript:示例和實際應用May 09, 2025 am 12:01 AM

HTML、CSS和JavaScript在網頁開發中的作用分別是:1.HTML用於構建網頁結構;2.CSS用於美化網頁外觀;3.JavaScript用於實現動態交互。通過標籤、樣式和腳本,這三者共同構築了現代網頁的核心功能。

如何在標籤上設置lang屬性?為什麼這很重要?如何在標籤上設置lang屬性?為什麼這很重要?May 08, 2025 am 12:03 AM

設置標籤的lang屬性是優化網頁可訪問性和SEO的關鍵步驟。 1)在標籤中設置lang屬性,如。 2)在多語言內容中,為不同語言部分設置lang屬性,如。 3)使用符合ISO639-1標準的語言代碼,如"en"、"fr"、"zh"等。正確設置lang屬性可以提高網頁的可訪問性和搜索引擎排名。

HTML屬性的目的是什麼?HTML屬性的目的是什麼?May 07, 2025 am 12:01 AM

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外觀和互動,使網站互動,響應式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,類,類型,類型,和dissabledtransfransformformformformformformformformformformformformformformforment

您如何在HTML中創建列表?您如何在HTML中創建列表?May 06, 2025 am 12:01 AM

toCreateAlistInHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulletedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,casundfornthetthetthetthetthetthetthetttributefordforderfordforderforderentnumberingsnumberingsnumberingStys。

HTML行動:網站結構的示例HTML行動:網站結構的示例May 05, 2025 am 12:03 AM

HTML用於構建結構清晰的網站。 1)使用標籤如、、定義網站結構。 2)示例展示了博客和電商網站的結構。 3)避免常見錯誤如標籤嵌套不正確。 4)優化性能通過減少HTTP請求和使用語義化標籤。

您如何將圖像插入HTML頁面?您如何將圖像插入HTML頁面?May 04, 2025 am 12:02 AM

toinsertanimageIntoanhtmlpage,usethetagwithsrcandaltattributes.1)usealttextforAcccessibilityandseo.2)instementRcsetForresponSiveImages.3)applylazyloadingWithLoadingWithLoading =“ lazy” tooptimizeperformance.4)tooptimizeperformance.4)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具