Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery未添加按钮_Jquery_Html - Fatal编程技术网

jquery未添加按钮

jquery未添加按钮,jquery,html,Jquery,Html,我想在单击按钮时添加一个按钮,在单击新按钮时添加一个新按钮以提醒某些内容,但目前它没有在单击新按钮时提醒消息 那么我做错了什么呢 这是我到目前为止所拥有的 <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script type="text/javascript"&g

我想在单击按钮时添加一个按钮,在单击新按钮时添加一个新按钮以提醒某些内容,但目前它没有在单击新按钮时提醒消息

那么我做错了什么呢

这是我到目前为止所拥有的

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#first').bind('click', addbtn);
        $('#second').on('click', alert);

        function addbtn(){
            $('.box').html('<input type="submit" value="Click me" id="second" />');
        }
        function alert(){
            alert("works");
        }
    });
    </script>
</head>
<body>
    <input type="submit" value="Click me" id="first" />
    <div class="box"></div>
</body>
</html>

$(文档).ready(函数(){
$('first').bind('click',addbtn);
$(“#秒”)。在('click',alert);
函数addbtn(){
$('.box').html('');
}
函数警报(){
警惕(“工作”);
}
});

在您的示例中,您正在将事件处理程序绑定到“second”之前。因此,没有什么可以约束的

现在,因为您使用的是jQuery10+实时事件无法工作,所以必须使用on事件处理程序。现在ON事件处理程序附加到一个对象并提供一个选择器选项。在这种情况下,您希望在单击选择器#秒时触发并触发事件。如果我失去了你更多的细节在这里


$(文档).ready(函数(){
$('first').bind('click',addbtn);
$('.box')。在('单击','秒',函数()上){
警报(“工作”);
})
函数addbtn(){
$('.box').html('');
}
});
现在另一个问题是您定义了一个函数
alert()
,该函数调用内置函数
alert()
。在定义了函数之后,实际上已经重写了函数,并导致了无限递归。除非绝对需要,否则避免重写浏览器方法


干杯。

您正在动态创建元素,事件未绑定到您的案例中将来要添加的元素,因此您可以使用事件委派<代码>$('.box')。在('单击','秒',警报)上或绑定。可能的重复项仅用于添加到@PSL的注释,请使用以下格式作为粗略指南:
$(父级).on(事件、元素、回调)
<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#first').bind('click', addbtn);

        $('.box').on('click', '#second', function(){
            alert('works');
        })

        function addbtn(){
            $('.box').html('<input type="submit" value="Click me" id="second" />');
        }

    });
    </script>
</head>
<body>
    <input type="submit" value="Click me" id="first" />
    <div class="box"></div>
</body>
</html>