javascript - jquery code doesn't work in ajax loaded content - Stack Overflow

Yes, I looked trough same articles here, and google helped. But I'm a designer, not a coder. I can

Yes, I looked trough same articles here, and google helped. But I'm a designer, not a coder. I can copy\paste some code, even change something, but this problem is too hard for me.

So, I have news on my main page, and when you scroll down, next page loads with ajax. There is a script, that shows\hides rating bar for every post. But this code doesn't work inside the loaded content. Please, fix it, if it's not to hard for you.

p.s. I've tried to look into on(), or live, but as I sad before, not my level.

$(document).ready(function() {

var element = $('#dle-content .main-news');
for (i=0; i<element.length; i++)
    {
        $(element[i]).addClass('news-'+i);
    }

$('#dle-content .main-news').hover(
    function() {
        $('.main-news-hidden').stop(true);
        $(this).find('.main-news-hidden').animate({
            'bottom':'0'
        },400
        );
    }, function() {
        $('.main-news-hidden').stop(true);
        $(this).find('.main-news-hidden').animate({
            'bottom':'-120'
        }, 0);
        $('.main-news-hidden').stop(true);
});

$('.top-news li').hover(
    function() {
        $('.top-news-hidden').stop(true).fadeOut(0);
        $(this).find('.top-news-hidden').animate({
            'opacity':'1'
        },400, function()
            {
                $(this).fadeIn();
            }
        );
    }, function() {
        $('.top-news-hidden').stop(true);
        $(this).find('.top-news-hidden').fadeOut(100);
});

var element2 = $('.top-news li');
for (i=0; i<element2.length; i++)
    {
        $(element2[i]).find('.list').append(i+1);
    }

});

Yes, I looked trough same articles here, and google helped. But I'm a designer, not a coder. I can copy\paste some code, even change something, but this problem is too hard for me.

So, I have news on my main page, and when you scroll down, next page loads with ajax. There is a script, that shows\hides rating bar for every post. But this code doesn't work inside the loaded content. Please, fix it, if it's not to hard for you.

p.s. I've tried to look into on(), or live, but as I sad before, not my level.

$(document).ready(function() {

var element = $('#dle-content .main-news');
for (i=0; i<element.length; i++)
    {
        $(element[i]).addClass('news-'+i);
    }

$('#dle-content .main-news').hover(
    function() {
        $('.main-news-hidden').stop(true);
        $(this).find('.main-news-hidden').animate({
            'bottom':'0'
        },400
        );
    }, function() {
        $('.main-news-hidden').stop(true);
        $(this).find('.main-news-hidden').animate({
            'bottom':'-120'
        }, 0);
        $('.main-news-hidden').stop(true);
});

$('.top-news li').hover(
    function() {
        $('.top-news-hidden').stop(true).fadeOut(0);
        $(this).find('.top-news-hidden').animate({
            'opacity':'1'
        },400, function()
            {
                $(this).fadeIn();
            }
        );
    }, function() {
        $('.top-news-hidden').stop(true);
        $(this).find('.top-news-hidden').fadeOut(100);
});

var element2 = $('.top-news li');
for (i=0; i<element2.length; i++)
    {
        $(element2[i]).find('.list').append(i+1);
    }

});

Share Improve this question asked Aug 25, 2013 at 16:54 noidnoid 451 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Try to include your code in ajax html data , or you can add your script in done function after the html appended.

$.ajax({
  url: "yourUrl",     
}).done(function(data) {
   $("#yourId").html(data);   // Solution 1 :  your content is loaded with ajax, Also this data has your jQuery script
   // Solution 2:  Now start you can call your script via function or add directly here (only html data is needed)
   yourFunction();
   //Solution 3: Now add your jquery code directly
   $(".someClass").click(function(){
     //events
   });
});

These are three differents ways to accessing ajax data with jQuery.

I'm currently short on time, but I'll try my best.

When your .hover() etc are executed, they're bound to the actual element you're providing. Now, the elements that you load via AJAX are not available on the time your events are bound to your DOM elements.

One solution is the one already provided. However, there is IMHO a nicer one. The events you need are usually "bubbling", which means they get somewhat passed upwards along the DOM tree. You need to bind your events to a DOM element which does not get replaced/changed via AJAX, but is persistent. If you e.g. have a container #content in which your AJAX gets loaded into, you want to take this container as base for your event binding, because of that bubbling.

Try replacing your $('.someClass').click() with

$('#content').on('click', '.someClass', function(event) {
  // your event handling
}); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744750198a4591548.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信