如何在 JavaScript 警示視窗中顯示歐元或其他 HTML 實體?
本教程將教會我們如何在 JavaScript 警示視窗中顯示歐元或HTML 實體。JavaScript 中有三個彈出視窗:警示框、確認框和提示框。
警示框用於向用戶顯示一些資訊,例如歡迎訊息或使用者資訊。它包含一個確定按鈕,當用戶點選該按鈕時,它會關閉。
確認框用於顯示確認訊息。在許多應用程式中,您都看到過,當您嘗試刪除某些內容時,它會顯示確認訊息。當您點選確定時,它返回 true,如果您點選取消按鈕,它返回 false。因此,確認框有很多用途。
提示框用於使用彈出框從使用者那裡獲取輸入。
使用任何三個預設彈出框都不會允許使用者新增任何文字以外的內容。因此,我們應該有一個特殊的策略來將歐元圖示和其他實體新增到彈出框中。
在 HTML 5 中,有一些特殊字元,例如歐元、美元、盧比、不等於符號等。為了在 HTML 中顯示這些圖示,我們可以使用 HTML 實體,它是瀏覽器識別的任何元素的地址程式碼。
此外,我們還可以使用數字程式碼或十六進位制程式碼以字元格式顯示 HTML 實體。
語法
<script> alert('\u20AC'); // using the hexadecimal code alert('€'); // using the decimal code alert('&euro'); // using the HTML entity </script>
這裡\u20AC是十六進位制程式碼,@#8364是十進位制程式碼,&euro是歐元字元的 HTML 實體。
示例 1
使用十六進位制程式碼在警示框中顯示歐元字元
在下面的示例中,我們使用十六進位制程式碼 (\u20AC) 添加了歐元字元。
<html> <head> <title> Adding euro and other HTML entity to the Alert Box. </title> </head> <body> <h2> Adding euro and other HTML entity to the Alert Box. </h2> <script> // showing euro using the hexadecimal code alert("The price of the water is \u20AC 20"); // showing euro using the HTML entity document.write('The values of app is \u20AC 3000') // showing euro using the Numeric code document.write(' The product price is \u20AC 20') </script> </body> </html>
在上面的輸出中,使用者可以在警示框中看到歐元符號。此外,我們還在螢幕上渲染了歐元符號。
示例 2
使用十進位制程式碼在警示框中顯示歐元
在下面的示例中,我們使用十進位制程式碼 (€) 添加了歐元字元。我們需要在程式碼中新增 jQuery 才能在警示視窗中顯示歐元字元。要在簡單的 HTML 文件中顯示歐元字元,我們不需要新增 jQuery。
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script> </body> <h3> Adding euro and other HTML entity to the Alert Box. </h3> <p id = "demo"> The values of app is € 3000. The product price is € 20.</p> <script > var encoded = "Demo to show € in alert "; var decoded = $("<div/>").html(encoded).text(); alert(decoded); </script> </html>
示例 3
使用 HTML 實體將歐元新增到警示框中
在下面的示例中,我們使用十進位制程式碼 (&euro) 添加了歐元圖示。我們需要在程式碼中新增 jQuery 才能在警示視窗中顯示歐元字元。要在簡單的 HTML 文件中顯示歐元字元,我們不需要新增 jQuery。
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script> </body> <h2> Adding euro and other HTML entity to the Alert Box. </h2> <p id = "demo"> The values of app is € 3000. The product price is € 20.</p> <script > var encoded = "The price of water is € 20. "; var decoded = $("<div/>").html(encoded).text(); alert(decoded); </script> </html>
在 HTML 文字中,使用者需要新增字元的十六進位制程式碼才能使用特殊字元或 HTML 實體。但是,使用數字程式碼、編碼 URI 或 HTML 實體也是可能的。使用者可以在網際網路上找到任何特殊字元的 HTML 實體或十六進位制程式碼。
但是,如果使用者想要,他們可以建立一個自定義警示框併為特殊字元新增影像圖示。建議使用字元而不是圖示。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP