jquery事件不适用于动态元素
点击按钮,我添加了动态元素,例如:具有一些id的锚标记, 单击动态元素时,应打开未发生的警报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
<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@别担心,慢慢来,好好学习。快乐编程。