简单的window.alert()案例

window.alert()

弹出警告框来显示数据。

代码示例:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
  </head>
  <body>
    <h1>window表示电脑,alert表示弹窗。合起来的意思就是(电脑弹窗)</h1>
  </body>
</html>
<script>
  window.alert(5 + 6);
</script>

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个使用 HTML、CSS 和 JavaScript 实现了 alert()、prompt()、confirm()、print()、close、open() 功能的简单完整案例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JS Functions Demo</title> <style type="text/css"> body { font-family: Arial, sans-serif; font-size: 16px; line-height: 1.5; padding: 20px; } h1 { margin-bottom: 20px; } button { padding: 10px; margin-right: 10px; font-size: 16px; } </style> </head> <body> <h1>JS Functions Demo</h1> <button onclick="showAlert()">Show Alert</button> <button onclick="showPrompt()">Show Prompt</button> <button onclick="showConfirm()">Show Confirm</button> <button onclick="printPage()">Print Page</button> <button onclick="openPage()">Open Page</button> <button onclick="closePage()">Close Page</button> <script type="text/javascript"> // alert() function function showAlert() { alert("Hello, World!"); } // prompt() function function showPrompt() { var name = prompt("What is your name?"); if (name != null && name != "") { alert("Hello, " + name + "!"); } } // confirm() function function showConfirm() { var result = confirm("Are you sure you want to proceed?"); if (result == true) { alert("You clicked OK!"); } else { alert("You clicked Cancel!"); } } // print() function function printPage() { window.print(); } // open() function function openPage() { window.open("https://www.google.com/"); } // close() function function closePage() { window.close(); } </script> </body> </html> ``` 此案例中包含了 6 个按钮,分别对应了 alert()、prompt()、confirm()、print()、open()、close() 函数。点击不同的按钮,会触发相应的 JavaScript 函数,实现相应的功能。注意:在实际使用中,可能还需要进行一些额外的安全性和兼容性处理。 ### 回答2: 案例如下: ``` <!DOCTYPE html> <html> <head> <title>JS功能案例</title> </head> <body> <script> // 实现alert()功能 function customAlert(message) { alert(message); } // 实现prompt()功能 function customPrompt(message) { return prompt(message); } // 实现confirm()功能 function customConfirm(message) { return confirm(message); } // 实现print()功能 function customPrint(content) { document.write(content); } // 实现close功能 function customClose() { if(confirm("确定关闭当前页面吗?")) { window.close(); } } // 实现open()功能 function customOpen(url) { window.open(url); } // 测试以上功能 function testFunctions() { customAlert("这是一个alert弹窗"); var inputValue = customPrompt("请输入一段文字:"); customPrint("你输入的文字是:" + inputValue + "<br>"); var isConfirmed = customConfirm("你确认要删除吗?"); customPrint("你的选择是:" + isConfirmed + "<br>"); customPrint("网页内容将在3秒后关闭"); setTimeout(function() { customClose(); }, 3000); } // 运行测试函数 testFunctions(); </script> </body> </html> ``` 这个案例中,通过自定义函数实现了JavaScript中的常用功能:弹出提示框、弹出输入框、弹出确认框、打印内容到页面、关闭页面、打开新页面。最后通过调用`testFunctions()`函数来测试这些功能。 ### 回答3: 案例实现: 在HTML文件中,我们可以使用JavaScript语言实现alert()、prompt()、confirm()、print()、close、open()等功能。 1. alert()功能实现:在页面中添加一个按钮,点击按钮时弹出一个对话框,并在对话框中显示一条消息。代码如下: ```html <button onclick="alert('这是一条消息')">点击弹出消息</button> ``` 2. prompt()功能实现:在页面中添加一个按钮,点击按钮时弹出一个对话框,并在对话框中显示一个输入框,用户可以输入一条消息。代码如下: ```html <button onclick="prompt('请输入你的消息')">点击输入消息</button> ``` 3. confirm()功能实现:在页面中添加一个按钮,点击按钮时弹出一个对话框,并在对话框中显示一个确认框,询问用户是否确认执行某个操作。代码如下: ```html <button onclick="confirm('确定要执行操作吗?')">点击确认操作</button> ``` 4. print()功能实现:在页面中添加一个按钮,点击按钮时调用JavaScript的print()方法,实现打印当前页面的功能。代码如下: ```html <button onclick="window.print()">点击打印页面</button> ``` 5. close功能实现:在页面中添加一个按钮,点击按钮时调用JavaScript的close()方法,实现关闭当前窗口的功能。代码如下: ```html <button onclick="window.close()">点击关闭窗口</button> ``` 6. open()功能实现:在页面中添加一个按钮,点击按钮时调用JavaScript的open()方法,实现打开一个新窗口的功能。代码如下: ```html <button onclick="window.open('http://www.example.com')">点击打开新窗口</button> ``` 以上是一个简单完整的案例,实现了alert()、prompt()、confirm()、print()、close、open()等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值