嘿所以我试图让javascript / jquery将一些盒子div插入我的DOM中 - 无法弄清楚为什么它不起作用。小心协助我? 看看这里: http://codepen.io/anon/pen/gobcd
编辑(代码):
$document.ready(
for(i=0; i<17; i++){
$("#square_holder").append("<div class='block'></div>");
};
button.onclick=function(){
console.log("Prior grid setup cleared. Next you will be prompted for a new amount.")
var newnumber = gets.chomp;
console.log("You entered: #{newnumber}. Watch in awe as the grid fills ..... ")
};
);
答案 0 :(得分:6)
您的代码:
$document.ready(
for (i = 0; i < 17; i++) {
$("#square_holder").append("<div class='block'></div>");
};
button.onclick = function () {
console.log("Prior grid setup cleared. Next you will be prompted for a new amount.")
var newnumber = gets.chomp;
console.log("You entered: #{newnumber}. Watch in awe as the grid fills ..... ")
};
);
据我所知,你还没有定义$document
,我怀疑你的意思是:
$(document).ready(
接下来,您需要将函数引用传递给ready函数,因此它应该是:
$(document).ready(function() {
然后你还没有声明一个名为button
的变量,所以该行应该是:
$('button').click(function() {
// your code
});
进行所有这些更改后,新代码应该看起来像这样:
$(document).ready(function() {
for (i = 0; i < 17; i++) {
$("#square_holder").append("<div class='block'></div>");
};
$('button').click(function () {
console.log("Prior grid setup cleared. Next you will be prompted for a new amount.")
var newnumber = gets.chomp;
console.log("You entered: #{newnumber}. Watch in awe as the grid fills ..... ")
});
});
然后您的HTML存在一些问题。 <script>
标记可能会移至<head>
标记内,或者至少应移至</body>
标记之前。在相关的说明中,您正在尝试加载无法找到的样式表和脚本。
您还需要在<div>
之后将<div id="square_holder">
更改为</div>
,以便结束该div,而不是开始新的。