Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 只有当类名为==x时才加载ajax_Javascript_Jquery_Ajax_Class_If Statement - Fatal编程技术网

Javascript 只有当类名为==x时才加载ajax

Javascript 只有当类名为==x时才加载ajax,javascript,jquery,ajax,class,if-statement,Javascript,Jquery,Ajax,Class,If Statement,我编写了以下代码,希望它仅在内容尚未加载到div时加载ajax。基本上,当我使用ajax加载内容并根据当前加载的内容为内容指定类名时。如果当前类名是已经加载到div中的内容,那么我不加载ajax,否则使用ajax加载内容 现在不管div的类名是什么,ajax请求都会触发。知道这为什么不起作用吗 这里是网站:(点击左眼) 下面是javascript函数: else if(id =='left-eye23') {

我编写了以下代码,希望它仅在内容尚未加载到div时加载ajax。基本上,当我使用ajax加载内容并根据当前加载的内容为内容指定类名时。如果当前类名是已经加载到div中的内容,那么我不加载ajax,否则使用ajax加载内容

现在不管div的类名是什么,ajax请求都会触发。知道这为什么不起作用吗

这里是网站:(点击左眼)

下面是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');
                        }

                    }
else if(id=='left-ey23')
{       
左内容=$(“#左内容”);
content=leftcontent.attr('class');
如果(内容!=“照片”)
{       
警告(“内容不是照片”);
滑出(移动);
$(“#relativeContainer”).css('z-index','-1');
var-leftcontent;
$(“#左”).bind(“WebKittTransitionEnd mozTransitionEnd OrtTransitionEnd msTransitionEnd transitionend transitionend”,
函数(){
$.ajax({
url:'photos.php',
beforeSend:function(){
html('Now loding…');
},
}).完成(功能(数据){
html(数据);
leftcontent.addClass(“照片”);
}); 
});
}
else if(内容=‘照片’)
{
滑出(移动);
$(“#relativeContainer”).css('z-index','-1');
警报(“内容是照片”);
}
}

使用jQuery验证类的最佳方法是通过。如果元素有多个类,它将无法正确匹配您使用的
.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');
}

您确定要
==而不是
=?无论哪种方式,它都会触发ajax。