Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

101 #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

101 #25

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions 101-clickable-grid/clickableGrid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>clickableGrid</title>
<style>
* {
margin: 0;
padding: 0;
}

.box {
width: 450px;
height: 500px;
margin: 100px auto;
}

h3 {
width: 450px;
height: 35px;
margin-bottom: 15px;
text-align: center;
background-color: #bbada0;
line-height: 35px;
font-size: 14px;
border-radius: 5px;
}

input {
width: 20px;
height: 20px;
text-align: center;
}

.boxContent {
display: -webkit-flex;
/* Safari */
display: flex;
width: 450px;
height: 450px;
border: 1px solid #000;
flex-flow: row wrap;
align-content: space-between;
}

.row {
display: flex;
display: -webkit-flex;
justify-content: space-between;
flex-flow: row nowrap;
flex: 1 1 100%;
}

.item {
display: flex;
display: -webkit-flex;
justify-content: center;
margin: 5px;
flex: 1;
background: #bbada0;
text-align: center;
list-style: none;
border-radius: 2px;
}
</style>
<script>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以考虑把script放在</body>之前,而不是在head里面

window.onload = function() {
var input=document.querySelector("input");
var boxContent = document.querySelector(".boxContent");
//输入数字,回车生成相应的方块
input.addEventListener("change", function() {
var value1 = typeof(input.value) == "string" ? parseInt(input.value) : input.value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value1不是一个好名字
应该使用===

if (typeof(value1) != "number" || value1 < 0 || value1 > 11) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同理,使用!==

alert("输入有误,请输入1-10之间的数字");
} else {
while (hasChild(boxContent)) {
boxContent.removeChild(boxContent.children[0]);
}
var value = input.value
//利用文档碎片
var fragment = document.createDocumentFragment();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个可以有

for (var i = 0; i < value; i++) {
var div = document.createElement("div");
div.className = "row"
fragment.appendChild(div)
for (var j = 0; j < value; j++) {
var span = document.createElement("span");
span.className = "item";
var spantxt = document.createTextNode(i * value + j + 1);
span.appendChild(spantxt);
// div.appendChild(span);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

chooseSize(span, value);
fragment.appendChild(span);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

div和span到底是什么关系啊?很困惑

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<div class="row">
    <span class="item"></span>
    <span class="item"></span>
</div>
<div class="row">
    <span class="item"></span>
    <span class="item"></span>
</div>

这样子

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同学你的实际运行结果好像并不是这个样子~
screenshot00016

}
}
boxContent.appendChild(fragment);
}
})
consoleNum(boxContent)
}
//点击打印出当前数字利用事件委托
function consoleNum(parents) {
parents.addEventListener('click', function(event) {
var e = event || window.event;
var target = e.target || e.srcElement;
if (target.nodeName.toLowerCase() == "span") {
console.log(target.innerHTML);
}
})
}

function chooseSize(nodeLi, val) {
switch (val) {
case "1":
nodeLi.style.fontSize = "200px";
break;
case "2":
nodeLi.style.fontSize = "100px";
break;
case "3":
nodeLi.style.fontSize = "80px";

break;
case "4":
nodeLi.style.fontSize = "60px";

break;
case "5":
nodeLi.style.fontSize = "50px";

break;
case "6":
nodeLi.style.fontSize = "40px";
break;
case "7":
nodeLi.style.fontSize = "30px";

break;
case "8":
nodeLi.style.fontSize = "20px";
break;
case "9":
nodeLi.style.fontSize = "10px";

break;
case "10":
nodeLi.style.fontSize = "5px";

break;
}
}
//判断节点是否存在孩子节点
function hasChild(node) {
return node.children.length
}
</script>
</head>

<body>
<div class="box">
<h3>请输入行列数:<input type="text" ></h3>
<div class="boxContent">
</div>
</div>
</body>

</html>