\n

Windows onafterprint Event <\/h1>\n

This attribute works in IE and Mozilla<\/p>\n

国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

\n
目錄
前 5 個 HTML 事件屬性
1.窗口事件
2.表單事件
3. Key Board Attributes
?4. Mouse Event Attributes
5. Drag Event Attributes
Conclusion
Recommended Article
首頁 web前端 html教程 HTML 事件屬性

HTML 事件屬性

Sep 04, 2024 pm 04:17 PM
html html5 HTML Tutorial HTML Properties HTML tags

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

前 5 個 HTML 事件屬性

HTML 事件屬性

HTML 中提供了不同的事件變體。所有這些事件都有一個名為事件處理程序的小代碼塊,該代碼塊在執(zhí)行事件操作時觸發(fā)。它們附加到 HTML 元素。事件處理程序或事件偵聽器在 HTML 事件屬性中發(fā)揮著重要作用。讓我們看看全局聲明并應用于 HTML 元素的不同類型的事件屬性以及它們的詳細工作。主要使用四個主要事件屬性。他們是:

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

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

1.窗口事件

  • onafterprintEvent: 所有 Html 標簽都支持此屬性,并且在頁面開始打印并且具有單值腳本時起作用。這是 HTML 代碼的示例。此示例顯示了按下按鈕時的情況;它打印一條在對話框消息中打印的消息。

代碼:

<!DOCTYPE html>
<html>
<head>
<title>
Windows onafterprint Event
</title>
</head>
<body onafterprint="myfun()">
<h1>Windows onafterprint Event </h1>
<p>This attribute works in IE and Mozilla</p>
<body style = "text-align:center">
<script>
function myfun() {
alert("Document is being printed");
}
</script>
</body>
</html>

輸出:

HTML 事件屬性

  • onbeforeprint:它在打印之前起作用。該事件在打印過程后被觸發(fā)。以下是示例代碼。

代碼:

<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #9370DB;
background-color: #BC8F8F;
text-align: center;
padding: 20px;
}
p {
font-size: 2rem;
}
</style>
</head>
<body onbeforeprint="get()">
<h1> Attribute Demo</h1>
<p style="color:#0000FF;">trigger to print.</p>
<div class="show"></div>
<script>
function get() {
document.body.style.background = "#00BFFF";
}
</script>
</body>
</html>

輸出:

HTML 事件屬性

  • onerror: 當沒有元素存在時拋出錯誤時會觸發(fā)此函數(shù)。

代碼:

<!DOCTYPE html>
<html>
<body>
<img src="p.jpg" onerror="myFun()">
<p>hello world.</p>
<script>
function myFun() {
alert("problem with image loading.");
}
</script>
</body>
</html>

輸出:

HTML 事件屬性

  • onload:此函數(shù)有助于加載對象,并且可以很好地查看網頁是否正確加載。

代碼:

<!DOCTYPE html>
<html>
<head>
<title>onload event demo</title>
</head>
<body>
<img src="pic.jpg" onload="ldImg()" width="50" height="92">
<script>
function ldImg() {
alert("image loaded without error");
}
</script>
</body>
</html>

輸出:

HTML 事件屬性

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

代碼:

<!DOCTYPE html>
<head>
<title>onresize event</title>
</head>
<body>
<script type="text/javascript">
function cmg() {
alert('welcome to educba');
}
window.onresize = cmg;
</script>
</head>
<body>
<input type="button"? value="Click the button"
onclick="alert(window.onresize);">
</body>
</html>

輸出:

HTML 事件屬性

  • onunload:當網頁窗口關閉或用戶離開網頁時觸發(fā)此事件。下面的代碼會在用戶離開時卸載頁面,并發(fā)出感謝搜索的警報。此事件有時適用于所有瀏覽器。

代碼:

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

輸出:

HTML 事件屬性

2.表單事件

它與表單控件一起使用。以下是用戶與瀏覽器交互時發(fā)生的屬性。

  • onblur:當用戶的注意力離開表單窗口時發(fā)生此事件。以下示例采用小寫形式輸入,單擊提交按鈕時,會將輸出轉換為大寫形式。

代碼:

<!DOCTYPE html>
<html>
<head>
<title> Form onblur </title>
<style>
body {
text-align:center;
}
h1 {
color:pink;
}
</style>
</head>
<body>
<h1>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>
</body>
</html>

輸出:

HTML 事件屬性

HTML 事件屬性

  • onchange: 當用戶更改表單中的現(xiàn)有元素時,會發(fā)生此事件。當元素失去焦點時就會發(fā)生這種情況。

代碼:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML onchange</title>
</head>
<body>
<form name="example" action="">? <input type="text" name="rahul" onchange="alert('input is changed')"><br>
<label>select the dress color</label>
<select onchange="alert('You have changed the selection!');">
<option>pink</option>
<option>Yellow</option>
<option>White</option>
</select>
<p><strong>Note:</strong> Select any option </p>
<label>Describe yourself in short : </label>?? <br/><textarea cols="15" rows="7" name="details" onchange="alert('description has changed')">?? </textarea><br>
<button type="button" name="submit">Submit</button>
</form>
</body>
</html>

輸出:

HTML 事件屬性

  • onfocus: 當用戶關注網頁上的元素或輸入獲得焦點時,會啟用此屬性。下面的示例突出顯示了我們在字段中輸入的內容。

代碼:

<!DOCTYPE html>
<html>
<body>
<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>
</body>
</html>

輸出:

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.
<!DOCTYPE html>
<html>
<head>
<title> HTML oninput </title>
</head>
<style>
body {
text-align:center;
}
h1 {
color:red;
}
</style>
<body>
<h1> 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>
</body>
</html>

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:

<!DOCTYPE html>
<html>
<head>
<title> example oninvalid Event </title>
<style>
p {
color:orange;
}
body {
text-align:center;
}
</style>???? </head>
<body>
<p> HTML is used to create a web page</p>
<form? method="get">
Enter the name:
<input type="text" oninvalid="alert('Fill the text form!');" required>
<input type="submit" value="Submit">
</form>? </body>
</html>

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:

<!DOCTYPE html>
<html>
<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>
<body>
<ol>
<li>Form with input to reset and submit</li>
</ol>
<form action="" method="get" onreset="onRes()" onsubmit="onSub()">
<label>Enter input:<br /><input type="text" id="iv" oninvalid="onInva()" oninput="onInp()"></label><br /><br />
<input type="submit" value="press"> <input type="reset">
</form>
<div id="a_box"></div>
<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>
</body>
</html>

Output:

HTML 事件屬性

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

Code:

<!DOCTYPE html>
<html>
<body>
<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>
</body>
</html>

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:

<!DOCTYPE html>
<html>
<head>
<title>onselect demo</title>
<style>
h1 {
color:magenta;
}
body {
text-align:center;
}
</style>
<script>
function eduhtml() {
alert("text highlighted!");
}
</script>
</head>
<body>
<h1>EDUCBA Online tutorial</h1>
Text Box: <input type="text" value="onselectattribute: A well defined portal" onselect="eduhtml()">
</body>
</html>
</html>

Output:

HTML 事件屬性

  • onsubmit: The purpose of this event is to execute the action performed while pressing the submit button.

Code:

<!DOCTYPE html>
<html>
<body>
<head>
<title> Onsubmit Example</title>
</head>
<form action="demo_form.asp" onsubmit="myF()">
Enter name: <input type="text" name="fname">
<label>Email :</label>
<input id="email" name="email" type="text">
<input type="submit" value="Submit">
</form>
<script>
function myF() {
alert("The form was submitted");
}
</script>
</body>
</html>

Output:

HTML 事件屬性

3. Key Board Attributes

  • OnKeyDown: It is triggered when a user presses a down Arrow key.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Example for Onkeydown.</p>
<input type="text" onkeydown="mykedwn()">
<script>
function mykedwn() {
alert("key press is activated");
}
</script>
</body>
</html>

Output:

HTML 事件屬性

  • OnKeyPress: This event gets triggered when the user presses any key on the keyboard. Note: some browser doesn’t support pressing any key.

Code:

<!DOCTYPE html>
<html>
<body>
<p> This example shows when a user type in the text area it triggers an event </p>
<form>
<textarea onkeypress="alert('triggering onkeypress event.')" placeholder="Place the cursor inside the textarea and press a key."? " cols="30" rows="4" style="background-color:pink;">> </textarea> </form>
</body>
</html>

Output:

HTML 事件屬性

  • OnKeyUp: This attribute gets triggered when a user releases a cursor from the text field. Below comes the demonstration.

Code:

<!DOCTYPE html>
<html>
<body>
<p> This example transforms the character to lower case.</p>
Fill the name: <input type="text" id="jjj" onkeyup="mykey()">
<script>
function mykey() {
var g = document.getElementById("jjj");
g.value = g.value.toLowerCase();
}
</script>
</body>
</html>

Output:

HTML 事件屬性

?4. Mouse Event Attributes

This action triggers a mouse event when a mouse is pressed either from a computer or any external devices like a smartphone or tablet. Some of the mouse events are given below:

  • onclick:?It is triggered when a user presses the button over the mouse. An input example is given below to show the event while clicking the mouse.

Code:

<!DOCTYPE html>
<html>
<body>
<h1>HTML onclick Event</h1>
<p> Event plays a vital role in HTML.</p>
<button onclick="oncf()">Click </button>
<p id="sample"></p>
<script>
function oncf() {
document.getElementById("sample").innerHTML = "Hello World";
}
</script>
</body>
</html>

Output:

HTML 事件屬性

  • onmousemove: It is fired when a mouse is moved over an image in any direction.

Code:

<!doctype html>
<html>
<head><title> Event onmousemove demo</title>
</head>
<body>
<p>This event is activated when the pointer drags its direction.</p>
<body style="width:200px;height:80px;border:2px solid;" onmousemove="javascript:alert('mouse action');">Sample text</body>
</body>
</html>

Output:

HTML 事件屬性

  • Onmouseup: This event gives a notification when a user releases a button on an output.

Code:

<!DOCTYPE html>
<html>
<head>
<style>
body {
color:? "#ff0000";
height: 120vh;
background-color: #610000;
text-align: center;
}
.polygon {
float: right;
shape-inside: polygon(0 0, 0 200px, 100px 200px);
clip-path: polygon(0 0, 0 250px, 100px 300px);
height: 200px;
width: 200px;
background: linear-gradient(to bottom left, #7CFC00, #8B008B);
}
p {
margin: 30px auto;
}
</style>
</head>
<body>
<h1>HTML onmouseup Demo</h1>
<div class="polygon" onmouseup="mupfn()"></div>
<p> click below object</p>
<script>
function mupFn() {
document.querySelector('.polygon').style.transform = 'scale(2.2)';
}
</script>
</body>
</html>

Output:

HTML 事件屬性

  • Onmouseover:?Execute a JavaScript when moving the mouse pointer over an image

Code:

<!DOCTYPE html>
<html>
<body>
<title>Example demonstrating Onmouseover.</title>
<h1 id="sample" onmouseover="A()" onmouseout="B()">Mouse over </h1>
<script>
function A() {
document.getElementById("sample").style.color = "yellow";}
function B() {
document.getElementById("sample").style.color = "green";
}
</script>
</body>
</html>

Output:

HTML 事件屬性

5. Drag Event Attributes

This application helps in the HTML window when the user drags the input element. Below are the different event listeners used in HTML to store dragged data.

  • Ondrag: This is used when an element is being dragged from the web page.
  • Ondragstart: This fires when the user begins to drag from the input field. The below example demonstrates about dragging the two-target area.

Code:

<!DOCTYPE HTML>
<html>
<head>
<style type = "text/css">
#b1, #b2 {
float:left;padding:11px;margin:11px; -moz-user-select:none;
}
#b1 { background-color: #FF6699; width:65px; height:85px;? }
#b2 { background-color: #808000; width:180px; height:180px; }
</style>
<script type = "text/javascript">
function dStart(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData("Text", e.target.getAttribute('id'));
e.dataTransfer.setDragImage(e.target,0,0);
return true;
}
</script>
</head>
<body>
<center>
<h2>Drag demo</h2>
<div> Drag the box.</div>
<div id = "b1" draggable = "true"
ondragstart = "return dStart(e)">
<p>Drag it</p>
</div>
<div id = "b2">welcome</div>
</center>
</body>
</html>

Output:

HTML 事件屬性

  • ondrop:?Execute this attribute when a draggable element is dropped in
    element.

Code:

<!DOCTYPE HTML>
<html>
<head>
<style type = "text/css">
#b1, #b2 {
float:left;padding:11px;margin:11px; -moz-user-select:none;
}
#b1 { background-color: #FF6699; width:65px; height:85px;? }
#b2 { background-color: #808000; width:180px; height:180px; }
</style>
<script type = "text/javascript">
function dStart(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData("Text", e.target.getAttribute('id'));
e.dataTransfer.setDragImage(e.target,0,0);
return true;
}
</script>
</head>
<body>
<center>
<h2>Drag? dropdemo</h2>
<div> drop the box.</div>
<div id = "b1" draggable = "true"
ondragstart = "return dStart(e)">
<p>Drag it</p>
</div>
<div class="droptarget"
ondrop="drop(event)"
ondragover="allowDrop(event)">
</div>
<div id = "b2">welcome</div>
<span> dropzone </span>
</center>
</body>
</html>

Output:

HTML 事件屬性

Conclusion

This event attribute helps to make a web application very easier and attractive. The different occurrence of actions generates various events. Even though this approach is generally avoided, the programmer likes to learn the function assigned for the HTML attributes events. These event handlers are still executed to beautify the web pages.

Recommended Article

This is a guide to the HTML Event Attributes. Here we discuss the Introduction to HTML Event Attributes along with Code implementation and Output. you can also go through our suggested articles to learn more –

  1. HTML Frames
  2. HTML Style Attribute
  3. Applications of HTML
  4. HTML vs HTML5

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

本站聲明
本文內容由網友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

構建網頁的HTML元素是什么? 構建網頁的HTML元素是什么? Jul 03, 2025 am 02:34 AM

網頁結構需核心HTML元素支撐,1.頁面整體結構由、、構成,其中為根元素,存放元信息,展示內容;2.內容組織依賴標題(-)、段落()及區(qū)塊標簽(如、)以提升條理與SEO;3.導航通過與實現(xiàn),常用組織鏈接并輔以aria-current屬性增強可訪問性;4.表單交互涉及、、與,確保用戶輸入與提交功能完整。正確使用這些元素能提升頁面清晰度、維護性及搜索引擎優(yōu)化。

使用HTML5服務器序列事件處理重新連接和錯誤。 使用HTML5服務器序列事件處理重新連接和錯誤。 Jul 03, 2025 am 02:28 AM

使用HTML5SSE時,處理重連和錯誤的方法包括:1.了解默認重連機制,EventSource默認在連接中斷后3秒重試,可通過retry字段自定義間隔;2.監(jiān)聽error事件以應對連接失敗或解析錯誤,區(qū)分錯誤類型并執(zhí)行相應邏輯,如網絡問題依賴自動重連、服務器錯誤手動延遲重連、認證失效刷新token;3.主動控制重連邏輯,如手動關閉并重建連接、設置最大重試次數(shù)、結合navigator.onLine判斷網絡狀態(tài)以優(yōu)化重試策略。這些措施可提升應用穩(wěn)定性與用戶體驗。

為現(xiàn)代頁面宣布正確的HTML5 Doctype。 為現(xiàn)代頁面宣布正確的HTML5 Doctype。 Jul 03, 2025 am 02:35 AM

Doctype是告訴瀏覽器用哪種HTML標準解析頁面的聲明,現(xiàn)代網頁只需在HTML文件最開頭寫。其作用是確保瀏覽器以標準模式而非怪異模式渲染頁面,且必須位于第一行,前面不能有空格或注釋;正確寫法僅有一種,不推薦使用舊版本或其他變體;其他如charset、viewport等應放在部分。

使用HTML屬性實現(xiàn)客戶端表單驗證。 使用HTML屬性實現(xiàn)客戶端表單驗證。 Jul 03, 2025 am 02:31 AM

client-sideformvalidationCanbedOnewithOutJavaScriptbyusinghtmlattributes.1)useRequiredToEnforCemandatoryField.2)validateMailsAndUrllSwithTyPeatTributesLikeEmailOrurl,orusepatternwithRegegexforCustomAlorurl

用HTML5語義標記和微數(shù)據改善SEO。 用HTML5語義標記和微數(shù)據改善SEO。 Jul 03, 2025 am 01:16 AM

使用HTML5語義標簽和Microdata可提升SEO,因為它幫助搜索引擎更好理解頁面結構與內容含義。1.使用HTML5語義標簽如、、、、和來明確頁面區(qū)塊功能,有助于搜索引擎建立更準確的頁面模型;2.添加Microdata結構化數(shù)據標注具體內容,例如文章作者、發(fā)布日期、商品價格等,使搜索引擎能識別信息類型并用于富媒體摘要展示;3.注意正確使用標簽避免混淆、避免重復標記、測試結構化數(shù)據有效性、定期更新以適應schema.org的變化,并結合其他SEO手段長期優(yōu)化。

如何使用HTML將選項分組? 如何使用HTML將選項分組? Jul 04, 2025 am 03:16 AM

在HTML中使用標簽可以對下拉菜單中的選項進行分組。具體方法是用包裹一組元素,并通過label屬性定義組名,如:1.包含蘋果、香蕉、橙子等選項;2.包含胡蘿卜、西蘭花等選項;3.每個為一個獨立分組,組內選項自動縮進。注意事項包括:①不支持嵌套;②可通過disabled屬性禁用整個組;③樣式受限需結合CSS或第三方庫美化;可使用Select2等插件增強功能。

使用HTML按鈕元素實現(xiàn)可點擊按鈕 使用HTML按鈕元素實現(xiàn)可點擊按鈕 Jul 07, 2025 am 02:31 AM

要使用HTML的button元素實現(xiàn)可點擊按鈕,首先需掌握其基本用法與常見注意事項。1.使用標簽創(chuàng)建按鈕,并通過type屬性定義行為(如button、submit、reset),默認為submit;2.通過JavaScript添加交互功能,可內聯(lián)寫法或通過ID綁定事件監(jiān)聽器以提升維護性;3.利用CSS自定義樣式,包括背景色、邊框、圓角及hover/active狀態(tài)效果,增強用戶體驗;4.注意常見問題:確保未啟用disabled屬性、正確綁定JS事件、避免布局遮擋,并借助開發(fā)者工具排查異常。掌握這

將CSS和JavaScript與HTML5結構有效整合。 將CSS和JavaScript與HTML5結構有效整合。 Jul 12, 2025 am 03:01 AM

HTML5、CSS和JavaScript應通過語義化標簽、合理加載順序與解耦設計高效結合。1.使用HTML5語義化標簽如、提升結構清晰度與可維護性,利于SEO和無障礙訪問;2.CSS應置于中,使用外部文件并按模塊拆分,避免內聯(lián)樣式與延遲加載問題;3.JavaScript推薦放在前引入,使用defer或async異步加載以避免阻塞渲染;4.減少三者間強依賴,通過data-*屬性驅動行為、類名控制狀態(tài),統(tǒng)一命名規(guī)范提升協(xié)作效率。這些方法能有效優(yōu)化頁面性能與團隊協(xié)作。

See all articles