2016-10-17 71 views
0

點擊按鈕我添加動態元素例如:錨點標籤有一些ID, 點擊動態元素警報應該打開哪些沒有發生。jquery事件不適用於動態元素

<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> 
+3

[動態創建元素上的事件綁定?]可能重複?(http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Azim

回答

2

試試這個

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

$(document).on("click","#demo",function(){ 
    alert('In Demo....'); 
}); 

的問題是,您的活動未連接到的元素。

+0

真棒@Kinshuk Lahiri – Raman

+0

@Raman很高興幫助:) –

+0

@KinshukLahiri,如果你想添加多個,你使用的ID那裏id ='demo' – EaBangalore

相關問題