问题很奇怪,我本来想编一个辅助创建ui的js库,but,出错了
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
#area{
background-color: #AEEAAE;
width: 600px;
height: 450px;
}
#ball{
background-color: #BAF;
width: 30px;
height: 30px;
border-radius: 30px;
position: absolute;
top: 0px;
left: 0px;
}
</style>
</head>
<body>
<script>
var ui = {
writeDOM:function(td){
document.write();
document.write(td);
},
setTitle:function(txt){
document.getElementsByTagName("title")[0].innerHTML = txt;
},
newTitle:function(){
let title = document.createElement("title");
document.head.appendChild(title);
},
addTo:function(ele, to){
to.appendChild(ele);
},
labelAdd:function(label, input, p){
let ltg = document.createElement("label");
let txt = document.createTextNode(label);
ltg.appendChild(txt);
ltg.appendChild(input);
p.appendChild(ltg);
},
setSize:function setSize(who, size, wb, hb){
who.style.width =(size / 2 * wb)+ "px";
who.style.height =(size / 2 * hb)+ "px";
},
message:function(text, size, l, h, p){
let b1 = document.createElement("div"), b2 = document.createElement("div");
b1.setAttribute("class", "button1");
b2.setAttribute("class", "button2");
let agent = document.createElement("div");
agent.setAttribute("class", "agent");
agent.setAttribute("style", `position:relative;top:${h}px;`);
let txt = document.createElement("span");
txt.setAttribute("class", "txt");
txt.setAttribute("style", "position:absolute;top:30px;");
agent.appendChild(b1);
agent.appendChild(b2);
agent.appendChild(txt);
ui.setSize(agent, size, 2, 1);
agent.style.backgroundColor = "grey";
agent.style.left = l;
txt.innerHTML = text;
var x = l +(size / 2);
b1.setAttribute("style", "position:absolute;");
b2.setAttribute("style", "position:absolute;");
b1.style.left = '-1px';
b2.style.left = `${size / 2 + 1}px`;
b1.style.top = `${size / 2}px`;
b2.style.top = `${size / 2}px`;
var os = size / 2;
ui.setSize(b1, os, 2, 1);
ui.setSize(b2, os, 2, 1);
b1.style.backgroundColor = "#555";
b2.style.backgroundColor = "#555";
b1.setAttribute("onclick", `${b1}.remove;${b2}.remove;${txt}.remove;${agent}.remove;`);
b2.setAttribute("onclick", `${b1}.remove;${b2}.remove;${txt}.remove;${agent}.remove;`);
p.appendChild(agent);
}
}
//实例:
//这段代码用于添加并修改标题
ui.newTitle();
ui.setTitle("小球撞撞撞");
var ball = document.createElement("div");
var area = document.createElement("div");
ball.setAttribute("id", "ball");
area.setAttribute("id", "area");
//这段代码添加元素
ui.addTo(area, document.body);
ui.addTo(ball, area);
var inp = document.createElement("input");
//这段代码用来给input标签添加label
ui.labelAdd("控制方向>>>", inp, document.body);
//这段代码可以创造一个框
ui.message("欢迎你!", 250, 250, 0, document.body);
</script>
</body>
</html>
求答!