javascript - jquery load() on each() function working all together -
i working around ajax loading powered website . here trying load different url on different link there little bit problem facing . there used 4 links , 4 pages load each page linked on clicking link 4 pages loading .
my html is
<div class="col-md-12"> <a href="ajax/portfolio_single.php" data-content="ajax" class="ajax-call">this one</a> <a href="ajax/portfolio_single2.php" data-content="ajax" class="ajax-call">hello</a> <a href="ajax/portfolio_single3.php" data-content="ajax" class="ajax-call">hello</a> <a href="ajax/portfolio_single4.php" data-content="ajax" class="ajax-call">hello</a> <div class="ajaxshow"></div> </div> and javascripts are
(function($){ 'use strict'; var ajaxcontent = $('[data-content="ajax"]'), pathname = window.location.pathname, url = window.location.href, ajaxurl; ajaxcontent.each( function(index, el) { $( ).append('<span class="ajax-live"></span>'); ajaxurl = $(this).attr('href'); $(this).click(function(event) { event.preventdefault(); $( '.ajax-live' ).addclass('ajax-live-on'); $( ).after('<span class="ajax-close animated bounceinright"></span>'); $('.ajaxshow').append().load(ajaxurl); $('.ajaxshow').addclass('animated bounceinup'); $('.ajaxshow').css('display', 'block'); }); $(document.body).on('click', '.ajax-close', function( event ){ event.preventdefault(); $( '.ajax-live' ).removeclass('ajax-live-on'); $( ).removeclass('ajax-close'); $( '.ajaxshow' ).fadeout(600).slideup(); }); }); })(jquery); though hete each function not working doesnot working.can help?
a working example here http://orlandojoes.co.uk/website/ajaxtest.php
the problem ajaxurl variable within different closure 1 assume time clicked, fourth link.
this because first do
var ajaxcontent = $('[data-content="ajax"]'), pathname = window.location.pathname, url = window.location.href, ajaxurl; here, ajaxurl global 'each' closure. in order fix problem, remove ajaxurl declaration outside 'each' scope, , define within
var ajaxcontent = $('[data-content="ajax"]'), pathname = window.location.pathname, url = window.location.href; ajaxcontent.each( function(index, el) { $( ).append('<span class="ajax-live"></span>'); var ajaxurl = $(this).attr('href'); $(this).click(function(event) { . . . also change
$('.ajaxshow').append().load(ajaxurl); to
$('.ajaxshow').load(ajaxurl); there no need append load insert html response within ajaxshow div
Comments
Post a Comment