只能通過獲取html里的代碼或者創(chuàng)建全局變量來使用嗎?或者能在兩個函數(shù)之間調(diào)數(shù)據(jù)嗎?
<!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>
2021-03-07
兩個函數(shù)可以互相調(diào)用,但函數(shù)內(nèi)的數(shù)據(jù)大多作為臨時數(shù)據(jù),是不能進(jìn)行交換的