gpt4 book ai didi

jquery - 当#div.load 出错时向div 添加一个类?(Jquery)

转载 作者:行者123 更新时间:2023-11-28 02:10:56 25 4
gpt4 key购买 nike

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

$('#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);


}
});

谢谢,阿达什

最佳答案

假设根据您的选择器,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");
}
});

关于jquery - 当#div.load 出错时向div 添加一个类?(Jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com