0x1源代码
[JavaScript] 纯文本查看 复制代码 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>玖公子</title>
<style ></style>
<script type="text/javascript">
var len = 1;
//1P操作
function func1(a){
var d = document.getElementsByTagName("div")[2];
d.innerHTML = d.innerHTML + "<p>第" + len +"回合</p>";
if (a == 1) {
var x = Math.ceil(Math.random()*3);
if(x == 1)
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了攻击</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了攻击</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[0].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else if(x ==2)
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了攻击</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了防御</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[0].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了攻击</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了挑衅</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}
} else if(a == 2){
if(x == 1)
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了防御</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了攻击</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else if(x ==2)
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了防御</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了防御</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[0].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了防御</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了挑衅</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}
}
else
{
if(x == 1)
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了挑衅</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了攻击</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[0].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else if(x ==2)
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了挑衅</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了防御</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else
{
var p1 = "<p>玖公子对BOSS小菜鸟一枚发动了挑衅</p>";
var p2 = "<p>BOSS小菜鸟对玖公子一枚发动了挑衅</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[0].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}
}
}
//2P操作
function func2(a){
var d = document.getElementsByTagName("div")[2];
d.innerHTML = d.innerHTML + "<p>第" + len +"回合</p>";
if (a == 1) {
var x = Math.ceil(Math.random()*3);
if(x == 1)
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了攻击</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了攻击</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[1].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else if(x ==2)
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了攻击</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了防御</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[1].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了攻击</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了挑衅</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}
} else if(a == 2){
if(x == 1)
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了防御</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了攻击</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else if(x ==2)
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了防御</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了防御</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[1].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了防御</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了挑衅</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}
}
else
{
if(x == 1)
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了挑衅</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了攻击</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[1].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else if(x ==2)
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了挑衅</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了防御</p>";
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}else
{
var p1 = "<p>夏南离对BOSS小菜鸟一枚发动了挑衅</p>";
var p2 = "<p>BOSS小菜鸟对夏南离一枚发动了挑衅</p>";
var hp1 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[1].getElementsByTagName("a")[0];
hp1.innerText = hp1.innerText - 1;
var hp2 = document.getElementsByTagName("div")[1].getElementsByTagName("p")[2].getElementsByTagName("a")[0];
hp2.innerText = hp2.innerText - 1;
len = len + 1;
d.innerHTML = d.innerHTML + p1 + p2;
}
}
}
</script>
</head>
<body>
<div>
<p style="text-align: center;font-size: 36px;">玖公子学javascript</p>
<p style="font-size: 36px;">===============================================</p>
</div>
<div >
<p>
<button >玖  公  子 </button>血量:<a>10</a>
<button>攻击</button>
<button>防御</button>
<button>挑衅</button>
</p>
<p>
<button >夏  南  离 </button>血量:<a>10</a>
<button>攻击</button>
<button>防御</button>
<button>挑衅</button>
</p>
<p>
<button >BOSS小菜鸟一枚</button>血量:<a>100</a>
<button >攻击</button>
<button >防御</button>
<button >挑衅</button>
</p>
</div>
<div>
</div>
</body>
</html>
0x2效果图
0x3总结交流
1.document.getElementsByTagName(“标签名”)获取到的是一个数组,如果只有一个元素,也必须使用下标0来访问。
2.定义变量都是使用var,类型会自动匹配。
3.对于显示战斗内容的div,我每一个读取全部内容,再重新写回去,会不会影响执行效率,有没有办法直接在div内部的末尾直接插入标签?
4.程序字符串明显冗余,应该怎么优化,定义多个字符串感觉也麻烦。
PS:向@clearwater 大师学习编程,@夏南离 一起来学习打boss了! |