An append in jquery code and click function?
This is my following test.html file
<!DOCTYPE html>
<html>
<head>
<style>
p {
background:yellow;
}
.content{
width:40px;
margin:0px 30px;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(docu开发者_如何学运维ment).ready(function(){
$('#test').click(function(){
// alert("test");
var htmlData='<div class="resizable" ><div class="content">test</div></div>';
$('.container').append(htmlData);
});
$('.content').click(function(){
alert("test");
});
});
</script>
</head>
<body>
<div class="container">
<div class="resizable" >
<div class="selected content">Time</div>
<button type="button" id="test">Click Me!</button>
</div>
</div>
</body>
</html>
When i click on the button a div is appended i have also class content on which click function is written which called alert.The jquery click function for content class does calls to the the content class which is hardcode but no for the content class which is appended by the click function of buttton.
You need to use live function for DOM elements created dynamically - http://api.jquery.com/live/
$('.content').live('click', function(){
alert("test");
});
$("body").delegate("#test", "click", function(){
//write your code here
});
精彩评论