且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery事件不适用于ajax加载的内容

更新时间:2023-12-05 08:17:34

这是因为您要将事件绑定到准备就绪的文档上。您必须使用委托才能使其正常工作。就像上一样。这是因为.header加载时不在页面上的页面上。因此,没有附加事件。

It's because you are binding the event on document ready. You have to use a delegate in order for this to work. Like on. It's because .header isn't on the page on the page when it's loaded. So no event is attached.

您的代码应与以下内容相似:

Your code should look some along the lines of this:

$('body').on('click','.heading',function(){
     $(this).css('color','red');  
});   

它不必是主体,但是在准备好文档后不会加载的元素,是 .heading 的父级。

It doesn't have to be body, but an element which isn't loaded after document ready, which is a parent of .heading.