如何保持显示按钮?为何按钮会消失……
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>navigator</title>
<script type="text/javascript">
function validB(){
var u_agent = navigator.userAgent;
var B_name="不是想用的主流浏览器!";
if(u_agent.indexOf("Firefox")>-1){
B_name="Firefox";
}else if(u_agent.indexOf("Chrome")>-1){
B_name="Chrome";
}else if(u_agent.indexOf("MSIE")>-1&&u_agent.indexOf("Trident")>-1){
B_name="IE(8-10)";
}
document.write("浏览器:"+B_name+"<br>");
document.write("u_agent:"+u_agent+"<br>");
}
</script>
</head>
<body>
<form>
<input type="button" value="查看浏览器" onclick="validB()">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Math </title>
<script type="text/javascript">
function push(){
var x=(Math.random())*10;
document.getElementById('po').innerHTML=(Math.round(x));}
</script>
</head>
<body>
<span id="po"></span>
<div>
<input type="button"value="点击" onclick="push()">
</div>
</body>
</html>
消失原因是执行document.write时文档流已关闭,而document.write是打开文档流再写入,此时是一个新的页面,按钮在旧的页面上,所以看不到了。具体可以查查“为什么document.write()会清空原来的内容”,有前辈给了详细例子与解释。保持按钮显示的方法楼上给了。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>navigator</title>
<script type="text/javascript">
function validB(){
var u_agent = navigator.userAgent;
var B_name="不是想用的主流浏览器!";
if(u_agent.indexOf("Firefox")>-1){
B_name="Firefox";
}else if(u_agent.indexOf("Chrome")>-1){
B_name="Chrome";
}else if(u_agent.indexOf("MSIE")>-1&&u_agent.indexOf("Trident")>-1){
B_name="IE(8-10)";
}
document.getElementById('emps').innerHTML="浏览器"+B_name+"<br>"
document.getElementById('emps').innerHTML="u_agent"+u_agent+"<br>"
}
</script>
</head>
<body>
<form>
<input type="button" value="查看浏览器" onclick="validB()" >
<div id="emps"></div>
</form>
</body>
</html>