所以我通过名为ajaxLoad.php的文件中的foreach循环向index.php添加元素,使用jQuery的load()加载。
问题是当我尝试选择它们时,jQuery无法找到这些元素。我知道ajaxLoad.php还没有完成打印消息。当我尝试用jQuery选择删除类锚时,jQuery无法找到它们。
<div class="messages">
<!--these are added by the foreach loop-->
<a href="#" class="delete">Delete</a>
</div>
一个可能重要的注意事项是delete类不在index.php标记中,它们只能通过load()添加。这是内联jQuery:
<script>
$(function(){
//adds messages to a div
$('.messages').load('ajaxLoad.php');
//later on...
$('.delete').click(function(){
alert('Hey');
//nothing happens
});
});
</script>
我已经查看了$(window).load(function(){})但它似乎无法正常工作。 我很乐意帮忙!谢谢!
答案 0 :(得分:0)
将其置于回调函数中,请参阅manual:
$('.messages').load('ajaxLoad.php', function() {
$('.delete').click(function(){
alert('Hey');
});
});
或使用event delegation:
$('.messages').load('ajaxLoad.php');
//later on...
$('body').on('click', '.delete', function() {
// ^^^^ any containing element that is already there on page-load
alert('Hey');
});