如何在 JavaScript 警报窗口中显示欧元或其他 HTML 实体?

front end technologyjavascriptweb development

本教程将教我们在 JavaScript 警报窗口中显示 欧元HTML 实体。JavaScript 中有三个弹出窗口。警报框、确认框和提示框。

警报框可用于向用户显示一些信息,例如欢迎消息或用户信息。它包含 确定 按钮,当用户单击该按钮时,它会关闭。

确认框用于显示确认消息。在许多应用程序中,您已经看到当您尝试删除某些内容时,它会显示确认消息。当您单击确定时,它返回 true,如果您单击取消按钮,它返回 false。因此,确认框有很多用途。

提示框对于使用弹出框从用户那里获取输入很有用。

使用三个默认弹出框中的任何一个都不会允许用户添加除文本以外的任何内容。所以,我们应该有一个特殊的策略来将欧元图标和其他实体添加到弹出框中。

在HTML 5中,有一些特殊字符,例如欧元,美元,卢比,不等于符号等。要在HTML中显示这些图标,我们可以使用HTML实体,它是浏览器识别的任何元素的地址代码。 

此外,我们还可以使用数字代码或十六进制代码以字符格式显示HTML实体。

语法

<script>
   alert('\u20AC'); // using the hexadecimal code
   alert('&#8364'); // 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 实体或十六进制代码。

但是,用户可以根据需要创建自定义警告框并为特殊字符添加图像图标。最好使用字符而不是图标。


相关文章