代码改变世界

.size()的使用,点击增加更多

2011-12-17 22:41  youxin  阅读(241)  评论(0编辑  收藏  举报
<!DOCTYPE html>
<html>
<head>
<style>
body
{ cursor:pointer; min-height: 100px; }
div
{ width:50px; height:30px; margin:5px;
float
:left; background:blue; }
span
{ color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<span></span>
<div></div>

<script>
$(document.body)
.click(
function() {
$(
this).append( $("<div>") );
var n = $("div").size();
$(
"span").text("There are " + n + " divs. Click to add more.");
})
// trigger the click to start 本来只有一个,页面已载入事件就已经触发,显示2个
.click();
</script>

</body>
</html>