Home  >  Article  >  Web Front-end  >  Javascript realizes the effect of confirming and canceling the prompt box_javascript skills

Javascript realizes the effect of confirming and canceling the prompt box_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:50:461611browse

This effect is found on many web pages. When you click a button or other object, a prompt box will pop up. If you click OK, you will continue to execute the established program. If you click Cancel, you will cancel and continue execution. The code example is as follows:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>确定和取消提示框</title>
<style type="text/css">
div
{
 width:150px;
 margin:0px auto;
}
a
{
 font-size:12px;
 color:blue;
}
</style>
<script type="text/javascript">
window.onload=function(){
 var mylink=document.getElementById("mytest");
 mylink.onclick=function(){
  if(confirm("确定要访问吗"))
  {
    return true;
  }
  else
  {
    return false;
  }
 }
}
</script>
</head>
<body>
 <div><a href="/" id="mytest">点击跳转</a></div>
</body>
</html>

In the above code, when you click on the link, a prompt box will pop up. If you click OK, you will visit the Ant Tribe homepage, otherwise you will not visit. The core of realizing this function is to use confirm(), and the parameter value is the text content of the prompt box. Here is a brief introduction to the principle of the above code:

1. When a link is clicked, the onclick event handler bound to the a element will be called.

2. In the event processing function, since confirm() will return true when the OK button is clicked, and false when the cancel button is clicked, the if statement will choose to execute the if statement or else statement based on the return value of confirm(). .

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn