简体   繁体   中英

AJAX GET request content not responding to jQuery

I'm currently trying to display the memberlist of all my users with a AJAX get request. Everything is working fine, but the content it appends to my HTML does not respond to another jQuery script.

Is there a way to hardcode the retrieved content in my HTML?

Here's get request I'm using:

jQuery(document).ready(function() {
    $.get("../assets/includes/memberlist.php", function(data) {
        $(".to-show").html(data);
    });
});

The retrieved content has several divs in it, which I should be able to toggle. But this isn't working. If I view my source code, the appended data isn't visible in the source code.

Here's the script which should do the toggle trick:

jQuery(document).ready(function($) {

  var $mores = $('.event-toggle').hide();
  var $items = $('.event-title');

  var $titles = $('.event-title').click(function() {
    var $more = $(this).next('.event-toggle').slideToggle();
    $mores.not($more).slideUp();
    var $title = $(this).toggleClass('event-title active-title');
    $titles.not($title).removeClass('active-title').addClass('event-title')
    return false;
  });
});

Thanks for any help!

You won't be able to see any newly added snippets to the DOM by just viewing the source code of the page. You should be able to use Firebug or Chrome developer tools and then inspect the DOM to see the newly added content.

Also bear in mind that the JQuery GET request is asynchronous so you cannot rely on it being available in your second snippet. You should ideally have placed that code in a function and then called it when the GET request completed successfully.

jQuery(document).ready(function() {
    $.get("../assets/includes/memberlist.php", function(data) {
        $(".to-show").html(data);
        /* FUNCTION TO OPERATE ON NEWLY ADDED DOM SHOULD GO HERE */
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM