单击按钮后的无响应元素
Unresponsive elements after button click
背景:
前几天我启动了Javascript,并构建了这个草稿板:
http://frankpeelen.github.io/sketch-pad/
以下是关于的说明
http://www.theodinproject.com/web-development-101/javascript-and-jquery
问题:
它基本上完成了,除了使用"新建"按钮创建新网格外,其他操作都很好。突然之间,"方块"停止了对"悬停"事件的响应。我已经搜索过了,但找不到任何类似的问题。有什么想法吗?
代码:
$(document).ready(function() {
var build = function(h, w) {
var height = h;
var width = w;
//Loop through height to create rows
for (i = 1; i <= height; i++) {
$("#sketchpadcontainer").append("<div class='row'></div>");
//Loop through width to create divs in each row
for (j = 1; j <= width; j++) {
$(".row:last-child").append("<div class='sqrcontainer'><div class='square'></div></div>");
}
}
};
//Build default 16x16 grid
build(16, 16);
$("button").click(function() {
var size = parseInt(prompt("How many squares per side would you like? Please enter a number."));
//In case the number entered > 50
if (size > 50) {
$(".row").remove();
build(50, 50);
alert("The number you entered was too large. The number 50 has been used instead.")
}
//In case a non-number is entered
else if (isNaN(size)) {
$(".row").remove();
build(16, 16);
alert("You didn't enter a number. The default of 16 has been used.")
}
//In case a number <= 50 is entered
else {
$(".row").remove();
build(size, size);
}
})
$(".square").hover(
//Mouse in
function () {
$(this).css("background-color", "blue");
},
//Mouse out
function () {} );
});
html, body, #sitecontainer {
height: 100%;
width: 100%;
margin: 0px;
}
#sketchpadcontainer {
}
button {
display: block;
margin: 0 auto;
margin-top: 2em;
margin-bottom: 1em;
}
#sketchpadcontainer {
}
.row {
text-align: center;
}
.sqrcontainer {
height: 1.5em;
width: 1.5em;
display: inline-block;
}
.square {
width: 1em;
height: 1em;
margin: .1em;
border-color: black;
border-style: solid;
border-radius: .1em;
}
<!DOCTYPE html>
<head>
<title>Sketch Pad</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type='text/javascript' src="js/javascript.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="sitecontainer">
<div id="buttoncontainer">
<button type="button">New</button>
</div>
<div id="sketchpadcontainer">
</div>
</div>
</body>
您正在删除构建方法之前的div。这意味着悬停侦听器将被删除到。您必须再次将它们添加到.square元素中。
只需移动
$(".square").hover(
//Mouse in
function () {
$(this).css("background-color", "blue");
},
//Mouse out
function () {} );
在您的构建方法内部
更改$(".square").hover(
至$(document).on("hover" ,".square", function(){ . . . })
$(".square").hover(
中的Ur问题-在$(document).ready
之后工作,但在u添加新行之后-文档就绪不触发,并且Ur元素没有事件,并且触发。在页面上的每个元素上工作,甚至在动态添加的元素上。
相关文章:
- 通过iron-ajax初始化对象数组(链接到caller's元素的响应)
- 响应画布元素
- 为什么不是't html<对象>元素响应鼠标事件
- 如何在jquery ajax成功html响应中找到输入隐藏元素
- 按钮元素没有响应
- 单击按钮后的无响应元素
- 自动完成搜索无法正常工作,请不要迭代$中的所有元素.每个ul-li都会及时显示响应
- 原型js 如何使定期更新程序隐藏基于ajax响应的类元素
- Jquery-(ajax async:true)元素大小没有响应
- 为什么jquery元素在ipad上没有响应
- 页面元素停止响应
- 如何从html响应解析javascript数组(包含JSON对象作为元素)
- 循环遍历 AJAX 响应元素
- 多个元素响应移动Webkit和Hammer JS上的触摸事件
- 将元素添加到 Ajax 响应返回的表单中
- for 循环元素的执行时间比内部 AJAX 调用可以响应的要快
- 响应元素增长的事件
- 如何将 html 元素替换为 ajax 响应
- 响应元素显示:无;在移动宽度,然后通过javascript按钮返回,但显示的链接断开:block;
- 无法更改响应元素