Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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 - Fatal编程技术网

jquery事件不适用于动态元素

jquery事件不适用于动态元素,jquery,Jquery,点击按钮,我添加了动态元素,例如:具有一些id的锚标记, 单击动态元素时,应打开未发生的警报 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#submit").on("click",fu

点击按钮,我添加了动态元素,例如:具有一些id的锚标记, 单击动态元素时,应打开未发生的警报

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(document).ready(function(){

    $("#submit").on("click",function(){
         $("#op").html("<a href='javascript:void(0)' id='demo'>Click</a>");
    });
    $("#demo").on("click",function(){
        alert('In Demo....');
    });
});

</script>
</head>
<body>
<button id="submit">Submit</button>
<pre id='op' ></pre>
</body>
</html>

$(文档).ready(函数(){
$(“#提交”)。在(“单击”,函数(){
$(“#op”).html(“”);
});
$(“#演示”)。在(“单击”,函数(){
警报(“在演示中…”);
});
});
提交
试试这个

$(document).on("click","#submit",function(){
     $("#op").html("<a href='javascript:void(0)' id='demo'>Click</a>");
});

$(document).on("click","#demo",function(){
    alert('In Demo....');
});
$(文档)。在(“单击”,“提交”,函数()上){
$(“#op”).html(“”);
});
$(文档)。在(“单击”,“演示”,函数()上){
警报(“在演示中…”);
});
问题是您的事件未附加到元素。

请尝试此操作

$(document).on("click","#submit",function(){
     $("#op").html("<a href='javascript:void(0)' id='demo'>Click</a>");
});

$(document).on("click","#demo",function(){
    alert('In Demo....');
});
$(文档)。在(“单击”,“提交”,函数()上){
$(“#op”).html(“”);
});
$(文档)。在(“单击”,“演示”,函数()上){
警报(“在演示中…”);
});

问题是您的事件没有附加到元素。

Awesome@Kinshuk的可能副本的可能副本Lahiri@KinshukLahiri,如果您想添加多个,您使用的是id there id='demo'@EaBangalore,那么您应该使用class,而不是使用this@别担心,慢慢来,好好学习。快乐编程。真棒@KinshukLahiri@KinshukLahiri,如果您想添加多个,您使用的是id there id='demo'@EaBangalore,那么您应该使用class,而不是使用this@别担心,慢慢来,好好学习。快乐编程。