1

コンテンツがまだdivにロードされていない場合にのみ、ajaxがロードされることを期待して、次のコードを記述しました。基本的に、ajaxを使用してコンテンツをロードし、現在ロードされているコンテンツに応じてコンテンツにクラス名を付ける場合。現在のクラス名がすでにdivにロードされているコンテンツである場合は、ajaxをロードしません。それ以外の場合は、ajaxを使用してコンテンツをロードします。

現在、divのクラス名に関係なくajaxリクエストが発生します。これが機能しない理由はありますか?

これがサイトです:(左目をクリックしてください)

http://www.uvm.edu/~areid/homesite/index.html

javascript関数は次のとおりです。

else if(id =='left-eye23')
                    {       
                        leftcontent = $("#left-content");                       
                        content = leftcontent.attr('class');
                        if(content !== 'photos')
                        {       
                            alert("content is not photos"); 
                            SlideOut(move);
                            $("#relativeContainer").css('z-index','-1');
                            var leftcontent;
                            $("#left").bind("webkitTransitionEnd mozTransitionEnd oTransitionEnd msTransitionEnd transitionend", 
                            function(){ 
                                $.ajax({
                                url:'photos.php',
                                beforeSend: function(){
                                    leftcontent.html('<img src="loading.gif" /> Now loding...');
                                },
                                }).done(function(data){
                                    leftcontent.html(data);
                                    leftcontent.addClass("photos");
                                }); 
                                });
                            }
                        else if(content == 'photos')
                        {
                            SlideOut(move); 
                            $("#relativeContainer").css('z-index','-1');
                            alert('content is photos');
                        }

                    }
4

1 に答える 1

3

jQueryを使用してクラスを検証する最良の方法は、を使用すること.hasClass()です。.attr()要素に複数のクラスがある場合、使用しているテストと同等に正しく一致しません。

// If it doesn't have the class 'photos', do your AJAX call
if( !$("#left-content").hasClass('photos'))
{       
   alert("content is not photos"); 
  SlideOut(move);
 $("#relativeContainer").css('z-index','-1');
 // Then do the AJAX, etc...
}
// Otherwise do the other stuff...
// Unless you have another case not shown in your code above,
// this can just be a plain else {} rather than the else if {}
else if ($("#left-content").hasClass('photos'))
{
  SlideOut(move); 
  $("#relativeContainer").css('z-index','-1');
  alert('content is photos');
}
于 2012-11-01T01:33:36.643 に答える