-
Notifications
You must be signed in to change notification settings - Fork 133
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 xxlovett0706 #24
base: master
Are you sure you want to change the base?
101 xxlovett0706 #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>创建cell</title> | ||
<script src="jquery.min.js"></script> | ||
<style> | ||
.box{ | ||
display: flex; | ||
width: 400px; | ||
height: 400px; | ||
border: 1px solid #CCC; | ||
flex-direction: column; | ||
} | ||
.row{ | ||
flex: 1; | ||
display: flex; | ||
} | ||
.item{ | ||
background-color: #000; | ||
flex: 1; | ||
text-align: center; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
color: #FFF; | ||
border: 1px solid #FFF; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<script> | ||
function CreateCell(n){ | ||
this.n = n; | ||
this.init(); | ||
} | ||
|
||
CreateCell.prototype.init = function(){ | ||
//创建DOM元素 | ||
var box = $('<div class="box"></div>'); | ||
//插入DOM元素 | ||
$('body').prepend(box); | ||
//循环row元素 | ||
var num = 0; //创建cell内的数字 | ||
for(var i = 1; i<= this.n; ++i){ | ||
$('.box').append('<div id="row' + i + '" class="row"></div>'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 比较好的是把整个html都拼好了,然后一次性插入dom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 刚开始 考虑的是把整个html都拼好,但是发现格子没有插入的目标,上面的$box 是不是就是一个变量?然后把所有的内部元素先插入进去? |
||
for(var j = 1; j<= this.n; ++j){ | ||
$('#row'+ i +'').append('<span class="item">'+ num +'</span>'); | ||
++num; | ||
} | ||
} | ||
//绑定点击事件 | ||
$('span').on('click', function(e){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这样子每个span都绑定一个事件,太多了,还是用event delegation比较好 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 事件代理是不是应该写成: |
||
console.log($(this).html()); | ||
}); | ||
}; | ||
|
||
$(function(){ | ||
var createCell = new CreateCell(40); | ||
}) | ||
</script> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑使用
$box
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个$box指的是什么意思?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
想起来了,我说之前为什么不对,我创建变量是
var num = ....
,应该创建jQuery变量var $num = ...
这样