初学者 - 尝试让 jQuery 插入元素

beginner - trying to get jquery to insert elements

本文关键字:jQuery 插入 元素 初学者      更新时间:2023-09-26

嘿,所以我试图让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 ..... ")
};
);

您的代码:

$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 id="square_holder"> 后的<div>更改为 </div>,以便它结束该div,而不是开始一个新的div。