只能通过获取html里的代码或者创建全局变量来使用吗?或者能在两个函数之间调数据吗?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Math </title>
<style>input{width:40px;border: 1px solid black;}.div1{
background-color:#eee; width:90px;border: 1px solid black;}
button{border: 1px solid black;}</style>
<script type="text/javascript">//生命不息整活不止
function gu(monsterHealth,monsterAttack,playerHealth,playerAttack){//生命不息整活不止
var monsterName=["亡灵","血骷髅","炸弹人"];
var monster = monsterName[Math.round(Math.random()*2)];//生命不息整活不止
monsterHealth=Math.ceil(Math.random()*200);
monsterAttack=Math.ceil(Math.random()*30);//生命不息整活不止
playerHealth=Math.ceil(Math.random()*200);//生命不息整活不止
playerAttack=Math.ceil(Math.random()*30);
document.getElementById("monsterName").value=monster;//生命不息整活不止
document.getElementById("monsterHealth").value=monsterHealth;//生命不息整活不止
document.getElementById("monsterAttack").value=monsterAttack;
document.getElementById("playerName").value="GULAO";
document.getElementById("playerHealth").value=playerHealth;
document.getElementById("playerAttack").value=playerAttack;
}
function Attack(){
console.log("fffff");
var monsterHealth=document.getElementById("monsterHealth").value;
var playerAttack=document.getElementById("playerAttack").value;
var playerHealth=document.getElementById("playerHealth").value;
var monsterAttack=document.getElementById("monsterAttack").value;
console.log("monsterAttack:"+monsterAttack);
document.getElementById("monsterHealth").value=monsterHealth-playerAttack;
document.getElementById("playerHealth").value=playerHealth-monsterAttack;
playerHealth=playerHealth-monsterAttack;
monsterHealth=monsterHealth-playerAttack;
if(monsterHealth<=0&&playerHealth<=0){
var result="平局";
document.getElementById("result").value=result;
}else if(monsterHealth<=0){
var result="获胜";
document.getElementById("result").value=result;
}else if(playerHealth<=0){
var result="失败";
document.getElementById("result").value=result;
}
}
</script>
</head><!--生命不息整活不止-->
<body><!--生命不息整活不止-->
<div class="div1">
「<input type="text" id="monsterName">」<br>
血量:<input type="text" id="monsterHealth"><br>
攻击:<input type="text" id="monsterAttack">
<hr><!--生命不息整活不止-->
「<input type="text" id="playerName">」<br>
血量:<input type="text" id="playerHealth"><br>
攻击:<input type="text" id="playerAttack"><br>
</div>
<button onclick="gu()">重制</button>
<button onclick="Attack()">攻击 </button><br>
<input type="text" id="result">
</body>
</html>