JS确认框与提示框

如果您希望用户验证或接受某个东西,则通常使用“确认”框。
当确认框弹出时,用户将不得不单击“确定”或“取消”来继续进行。
如果用户单击“确定”,该框返回 true。如果用户单击“取消”,该框返回 false。
语法格式如下:
window.confirm(“sometext”);
window.confirm() 方法可以不带 window 前缀来编写。

确认框格式如下:

  <!DOCTYPE html>
    <html>
    <body>
    <h1>JavaScript 确认框</h1>
    <button onclick="myFunction()">试一试</button>
    <p id="demo"></p>
    <script>
    function myFunction() {
      var txt;
      if (confirm("Press a button!")) {
        txt = "您按了确定";
      } else {
        txt = "您按了取消";
      }
      document.getElementById("demo").innerHTML = txt;
    }
    </script>
    </body>
    </html>

提示框:
如果您希望用户在进入页面前输入值,通常会使用提示框。
当提示框弹出时,用户将不得不输入值后单击“确定”或点击“取消”来继续进行。
如果用户单击“确定”,该框返回输入值。如果用户单击“取消”,该框返回 NULL。
语法
window.prompt(“sometext”,“defaultText”);
window.prompt() 方法可以不带 window 前缀来编写。

提示框实例如下:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Prompt</h1>

<button onclick="myFunction()">试一试</button>

<p id="demo"></p>

<script>
function myFunction() {
  var txt;
  var person = prompt("请输入您的名字:", "哈利波特");
  if (person == null || person == "") {
    txt = "用户取消输入";
  } else {
    txt = "你好," + person + "!今天过得好吗?";
  }
  document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

另附上里一个博主JavaScript表单的链接:https://blog.csdn.net/zhouziyu2011/article/details/70214981
小尾巴:当人的情绪处于低潮时,对任何事情都提不起兴趣,要学会转移注意力。有些事情既然已经成为事实,就尝试着去接受,去面对。一个人不可能改变世界,世界也不会因你而改变,所能做的,就是适应世界,不钻牛角尖,不要和别人攀比。你的生活,应该有你自己的精彩。

发布了74 篇原创文章 · 获赞 27 · 访问量 9520

猜你喜欢

转载自blog.csdn.net/qq_42526440/article/details/100851286
今日推荐