jquery recognizing later inserted html
Hello guys i have this script:
function getList(){
$.ajax({
type: "POST",
url: "listaPogovorki.php",
success: function(msg){
$("#listaPogovorki").html(msg);
}});
}
getList();
with this i call this php script:
<table>
<col id="td1"/>
<col id="td2"/>
<col id="td3"/>
<col id="td4"/>
<tr>
<td >#ID</td>
<td >Pogovorka</td>
<td >Avtor</td>
<td >Izmeni</td>
</tr>
<?php $result = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['pogovorka'];?></td>
<td><?php echo $row['avtor'] ?></td>
<td><img class="editImage" src="images/icon_pencil.png" width="16px" height="16px" alt="<?php echo $row['id'];?>"></td>
</tr>
<?php }
mysql_close($con);
?>
</tabl开发者_JAVA百科e>
now the problem is that the script doesn't recognize the html returned from the php script so i can't make clicking on the images(from php script) to fire some code. For example if i make:
$(".editImage").click ....
it wont work.
How can i make this work?
You want to use the live
function:
$('.editImage').live('click', ....);
Or use another element as an event delegate
.
The output is not recognised because the html you have returned is not part of the original dom before the script was called.
Have a look at bind or live with jquery
You could place the $(".editImage").click
inside the AJAX success callback after updating the DOM with the new HTML or use the .live()
method:
$('.editImage').live('click', function() {
...
});
This is what the .live function is for - it binds even to elements that don't exist yet.
$(".editImage").live("click", function() {});
http://api.jquery.com/live/
精彩评论