2012-03-02 71 views
1

我有一个问题,因为我真的不明白...... 我有两个代码的功能,一个工作,其他没有,我真的想了解为什么,请...

当我这样做:

$("#divLogin").load("./affichage/choixFormat.html", function() { 
    $("#choix-box").fadeIn(300); 
}); 

它工作正常,它加载HTML的DIV和表演之后。

但我有代码我$就最佳剧本谁不工作:

$.ajax({ 
     url: "./affichage/choixFormat.html", 
     type: "POST", 
     async: false, 
     cache: true, 
     dataType: "html", 
     beforeSend: function() { 
     $("#loading").removeClass("hide"); 
     }, 
     timeout: 5000, 
     error: function(request, textStatus, errorThrown) { 
      console.log(request.status); 
     }, 
     success: function (msg, textStatus, request) { 
     $("#divLogin").html(msg); 
     } 
    }); 
$("#choix-box").fadeIn(300); 

的JavaScript尝试之前完成褪色的DIV加载我想...... 但我不明白为什么...我选择了异步:false! 而我成功后褪色!

我更喜欢使用$就因为我们可以用它做更多的事情...... 谁能esplain我为什么它不工作? 和JQuery的.load是工作的罚款:(

感谢


我错了,对不起,这两个解决方案不工作:( 我必须加载两个时间的DivLogin的淡入它:(

我也试着做。

request.done(function(msg) { 
    console.log("End of div loading"); 
    $("#choix-box").fadeIn(300); 
}); 

但它不工作太:( 我只有在控制台中LOG DOM加载HTML过长并且无法显示它......这就是为什么我必须首次加载它,并且在第二次脚本执行时。

谁有其他的解决办法吗?


现在,我尝试的东西太多了...最好的时刻是:

$('#div').ready($("#div").fadeIn(300)); 

但它并不总是完美的太:( 在jQuery,请

真的没有真正的解决办法?

确定这里我最后的代码谁并不总是显示我的div :( 有些时候是的,有的时候没有,无法理解为什么呢?

$('body').prepend('<div id="divChoiceFormat"></div>'); 
//getContenu('./affichage/choixFormat.html', 'divChoixFormat'); 

// CSS Load 
appendCss("./style/choiceFormat.css", "choiceFormatCSS") 

$("#divChoiceFormat").load("./affichage/choiceFormat.html", function() { 

    // When ready 
    $('#divChoiceFormat').ready($("#choice-box").fadeIn(300)); 
} 

为HTML之前加载CSS我做:

function appendCss(url, id) { 

    var link = $("<link>"); 
    link.attr({ 
     charset: "UTF-8", 
     media: "all", 
     type: 'text/css', 
     rel:  'stylesheet', 
     id:  id, 
     href: url 
    }); 
    $("head").append(link); 

return false; 
} 

也许我的订单是假的?

+0

如果你使用一个id作为选择器,它必须是整个HTML中的uniq id ......如果你有多个id =“#choix-box”,你的脚本不能正常工作。 而不使用ajax调用html ... .load()就足够了。 – pirs 2014-09-06 08:48:55

回答

0
// before sent 

// call 
$("#divLogin").load("./affichage/choixFormat.html", function(response, status, xhr) { 

    if (status == "error") { 
     console.log('Error : '+ xhr.status + " " + xhr.statusText); 

    }else if(status == "success"){ 
     $("#divLogin").html(response); 
     $("#choix-box").fadeIn(300); 

    }else if(status == "notmodified"){ 
     console.log('Error : similar html detected'); 

    }else{ 
     console.log('Error : no status'); 
    } 
}); 

那东北角的作品。 如果没有,你的html中还有其他的东西。

  • 检查身份证,如果他们uniq的所有块,也可以使用一个类 倍数多块调用。
  • 检查您的相对路径也: './ affichage/choixFormat.html'
  • 最后,缓存在一个实例应对重复的load() efficently。
  • 请不要在ajax调用中使用Html ...有史以来最糟糕的做法。

涂peux我parler法语司徒veux mieux德放任comprendre,JE pense阙CA vient德吨标识魁n'est PAS独特政变河畔。

1

将这个$("#choix-box").fadeIn(300);阿贾克斯成功里面。

success: function (msg, textStatus, request) { 
     $("#divLogin").html(msg); 
     $("#choix-box").fadeIn(300); 
} 
+0

不工作太... – Fabien 2012-03-03 00:56:40

+0

请问你有其他的解决方案吗... 我觉得我没有真正了解如何知道Ajax命令是如何处理结果的! – Fabien 2012-03-04 16:13:41

+0

'$(“#loading”)。addClass(“hide”);'也应该添加到你的成功函数中。除此之外,我认为我们需要看看你的'HTML'。 – shaun5 2012-03-04 16:58:44