jQuery:append()不是't在选择器中插入任何元素
jQuery: append() isn't inserting any elements into selector
首先,这是我的代码。
var size = 400
function createGrid(size) {
$("#create").click(function() {
for (i = 0; i <= size; i++) {
$("#container").append('<div class="grid"> </div>');
}
});
}
$(document).ready(createGrid);
#container {
top: 10px;
position: relative;
width: 960px;
height: 960px;
border-color: black;
margin: auto;
outline: 2px solid black;
}
.grid {
display: inline-block;
border: 1px solid white;
background-color: black;
float: left;
width: 10px;
height: 10px;
}
<!DOCTYPE html>
<html>
<title>
Sam's Etcha Sketch
</title>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css">
</head>
<body>
<div>
<button id="create">Create!</button>
</div>
<div id="container"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>
我的目标是在单击按钮#create
后在#container
内创建方形div。我不介意它现在看起来有多难看,我只想点击按钮添加方块(目前还不是结果)。我检查了JS Bin和我的浏览器控制台是否有任何错误,但似乎找不到任何错误。我不确定我做错了什么,因为我在按钮上尝试了一个简单的淡出功能,但它似乎不起作用,所以也许这是我将放入HTML的方式?(我也试着把它放在里面。)
TL;博士
我的代码出了什么问题,导致我的click()
函数没有在容器中附加任何方形div?
您永远不会将size
传递给createGrid
这是你的密码。
// the outer "size" variable
var size = 400
// this creates a new "size" variable which shadows the outer one
function createGrid(size) {
$("#create").click(function() {
for (i = 0; i <= size; i++) {
$("#container").append('<div class="grid"> </div>');
}
});
}
// this passes "createGrid" to the event handler, which calls it without any argument
$(document).ready(createGrid);
以下是操作方法:
// this generates an event handler with a custom "size" in its scope
function getGridCreator(container, size) {
return function () {
$("#create").click(function() {
for (i = 0; i <= size; i++) {
$(container).append('<div class="grid"> </div>');
}
});
};
}
// this passes the grid creator to the event handler, which again calls
// it without any argument, but this time "size" is in scope
$(document).ready(getGridCreator("#container", 400));
一般提示:避免使用全局变量,而是使用函数参数和闭包。
试试这个,我在你的脚本中做了一些更改
$(document).ready(function(){
var size = 400
function createGrid(size) {
$("#create").click(function() {
for (i = 0; i <= size; i++) {
$("#container").append($('<div class="grid"/>'));
}
});
}
createGrid(size)
});
#container {
top: 10px;
position: relative;
width: 960px;
height: 960px;
border-color: black;
margin: auto;
outline: 2px solid black;
}
.grid {
display: inline-block;
border: 1px solid white;
background-color: black;
float: left;
width: 10px;
height: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<title>
Sam's Etcha Sketch
</title>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css">
</head>
<body>
<div>
<button id="create">Create!</button>
</div>
<div id="container"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>
相关文章:
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 在动态创建的元素上获取对特定选择器的引用
- AngularJS-在JSON选择器中使用变量名
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- jQuery最近父级的数据属性选择器
- 为什么不'在JQuery中找到第二个css选择器的工作
- 换行符插入jquery选择器
- jQuery:append()不是't在选择器中插入任何元素
- 如何在第n个shild('')选择器中插入变量
- jQuery 日期选择器不适用于动态插入的 html 元素
- 如何使用JQuery将颜色选择器插入到html中
- 在更改时,即使是动态插入的输入标记上的日期选择器的处理程序也不会;不起作用
- 如何在jquery向导中插入日期选择器
- JQuery:插入"()"在:contains()选择器中
- 在选择器中插入函数参数
- 将变量插入jQuery选择器
- 如何使用选择器在元素之后插入一个新元素
- 如何为所有jQuery选择器插入前缀?
- js日期选择器插入年份两次