Jump to content
php.lv forumi
  • 0
Sign in to follow this  
ziedinjsh

izvēlne ar pushstate,popstate

Question

Labdien, man ir tāda lieta, ka izveidoju kodu:

jQuery(function(){

	document.title = 'Braucam';

	jQuery('.menu li a').on('click', function(e){
	e.preventDefault();
	
	var url = jQuery(this).attr('href');
		jQuery.ajax({
			url:url, 
			success: function(data){
				var page_content = jQuery(data);
				var page_title = page_content.find('.data-title').text();
				var get_content = page_content.find('.data-content').html();
				
					jQuery('div#page').html(get_content);
					document.title = page_title;
					
					jQuery('.menu li.current').removeClass('current');
					jQuery(this).parent('li').addClass('current');

			}, 
			error: function(){
				alert('ups!');
			}
			});
			
		if(url!=window.location){
			window.history.pushState({path:url}, '', url);
		}		
	});
	return false;
	
	jQuery(window).bind('popstate', function(){
		jQuery.ajax({
			url:location.pathname, 
			success: function(data){
				var page_content = jQuery(data);
				var page_title = page_content.find('.data-title').text();
				var get_content = page_content.find('.data-content').html();
				
					jQuery('div#page').html(get_content);
					document.title = page_title;	
					
					jQuery('.menu li.current').removeClass('current');
					jQuery(this).closest('li').addClass('current');
			}
		});
	});
	

});

pārvietojoties par portālu it kā strādā, nu vismaz netiek pārlādēta lapa un saturs tiek ielādēts, bet ja es nospiežu ,back` pogu browserim tad mainās tikai url, bet ne lapas saturs. Saturs paliek tas pats.

 

Un vēlviena neliela problēma: Es nesaprotu kapēc nestrādā addClass('current') removeClass strādā.

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

this ir koteksta objekts un katrā funkcijā tas var būt cits un šajā gadījumā ajax response callā, tas noteikti ir kaut kas cits.

Lai to atrisinātu, vietā, kurs this ir a elements, vajag to saglabāt mainīgajā, piemēram:

var $a=$(this);

un tālāk lietot $a;

Edited by codez

Share this post


Link to post
Share on other sites
  • 0

Vai context, vai $.proxy, kā minēts tajā linkā augstāk.

Share this post


Link to post
Share on other sites
  • 0

`var that = this;`, protams, ir daudz stilīgāk.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...