使用 jQuery .load() 加载的内容不起作用

Content loaded with jQuery .load() doesn't function

本文关键字:不起作用 加载 jQuery load 使用      更新时间:2023-09-26

我在让JQuery加载html页面时遇到了一些问题。我使用 JQuery 加载此页面 vvv

<a class="codaw" href="#"><img id="codawx_img" src="img/codawx.png" alt="Call of Duty:Advanced Warfare" style="height:150px;"></a>
<script>
$('a.codaw').on('click', function(e){
e.preventDefault();
$('#container').remove();
$('#content').load('second.html'+'#content').hide().fadeIn('slow');
});
</script>

并且该页面加载正常,但^^^ jQuery函数不会加载第二个HTML内容,它只是加载href。

我不知道我在这里做错了什么,如果有人可以分享一些知识,我会感激不尽。

您可能需要事件委派。试试这个:

<script>
$(function(){
    $(document).on('click', 'a.codaw', function(e){
        e.preventDefault();
        $('#container').remove();
        $('#content').load('second.html'+'#content').hide().fadeIn('slow');
    });
});
</script>

参考:

http://davidwalsh.name/event-delegate