2017-07-06 42 views
1

当我按下按钮的一切正常,但按下输入它的触发功能,并在我的浏览器快速返回,在snippest它desapiar。我尝试了几种方式时按回车键,但我得到同样的问题,触发按钮 当触发器输入时执行速度快并返回?

function calculate() { 
 
      
 
      
 
     var response = document.getElementById("text").value; 
 
    
 
    if (response ==0 || response =="" || response >100 || response < 0) { 
 
     alert('you should write a 0<number<100'); 
 
     
 
     
 
    } else{ 
 
    
 
    var start = new Date(); 
 
    var date = start.getFullYear(); 
 
     
 

 
    
 
    var result = date - response; 
 
    
 
    x.innerHTML = 'Your Birth is '+ result; 
 
    document.getElementById('ma').style.color = 'lightpink'; 
 
    document.getElementById('ma').style.fontFamily = 'sans-serif'; 
 
    document.getElementById('ma').style.fontSize = "45px"; 
 
     
 
    
 
     
 
    } ; 
 
    
 

 

 
    
 
    
 

 
}; 
 

 
var x= document.getElementById('ma'); 
 
document.getElementById("button").onclick = function(){calculate();}; 
 

 

 
     
 
body { 
 
    outline: none; 
 
} 
 
h1 { 
 
    font-family: fresca; 
 
    outline: none; 
 
    font-size: 35px; 
 
    color:cornflowerblue; 
 
    
 
} 
 
form { 
 
    margin-top: 20em; 
 
    outline: none; 
 
} 
 

 
#text{ 
 
    width: 35em; 
 
    height: 3em; 
 
    padding-left: 20px; 
 
    border-radius: 5px; 
 
    outline: none; 
 
    
 
} 
 
#text:focus { 
 
    background: lightblue; 
 
} 
 
#button{ 
 
    width: 80; 
 
    height: 3em; 
 
    text-align: center; 
 
    border-radius: 10px; 
 
    border-style: none; 
 
    background: lightblue; 
 
outline: none; 
 
} 
 

 
#button:hover { 
 
    background: lightgreen; 
 
    
 
}
<html> 
 

 
<head> 
 
<link rel="stylesheet" type="text/css" href="css.css"> 
 
<link href="https://fonts.googleapis.com/css?family=Fresca" rel="stylesheet"> 
 
</head> 
 

 

 

 

 
<body> 
 
    <center> 
 
    
 
    <form> 
 
     <input type="text" placeholder="Your Age" id="text" onkeypress="Javascript: if (event.keyCode==13) calculate();" > 
 
     <input type="button" id="button" value="Calculate"> 
 
    </form> 
 
     <h1 id="ma" >Manaf</h1> 
 
     
 
    </center> 
 
    
 
    
 
<script src="script.js";></script> 
 
    
 
<script 
 
    src="https://code.jquery.com/jquery-3.2.1.min.js" 
 
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" 
 
    crossorigin="anonymous"></script> 
 
</body> 
 
</html>

回答

-1
<h1 id="ma" >Manaf</h1> 

你好,因为你是在表单这就是包裹为什么它的触发输入

相关问题