当#div.load出现错误时,为div添加一个类?(Jquery)

时间:2011-12-08 05:23:18

标签: jquery css asp.net-mvc-3 html

当div.load错误输出时,我需要在红色中显示我的错误消息,如代码中所示。如果成功,我会按原样呈现内容,我该怎么做?

$('#TermsAndConditions').load(termsconditionsurl, null, function (response, status, xhr) {
    if (status == "error") {
        var msg = "Sorry there was an error: ";
    $(this).addClass("error");

            $("#TermsAndConditions").html(msg + xhr.status + " " + xhr.statusText);


    }
});

谢谢, 阿达什讷

1 个答案:

答案 0 :(得分:1)

假设div根据你的选择器有ID“TermsAndConditions”,你会这样做:

if (status == "error") 
{         
  var msg = "Sorry there was an error: ";     
  $(this).addClass("error")
} 

在css中有:

.error
{
  color: red;
}

编辑:

你应该把它放在文件就绪功能中:

$(document).ready(function() {
  var div = $("#TermsAndConditions");

  if (status == "error")
  {
   var msg = "sorry there was an error:";
   div.val(msg);
    div.addClass("error");
  }
});