gpt4 book ai didi

jQuery 相当于 PHP 的 file_exists()?

转载 作者:行者123 更新时间:2023-12-01 01:39:35 25 4
gpt4 key购买 nike

在下面的代码片段中,从我的 jQuery 设置中,我需要检查图像文件是否确实存在,如果不存在,我想替换默认图像。目前,如果文件不存在,我只会得到一个损坏的图像占位符...

$('#myTheme').change
(
function()
{
var myImage = $('#myTheme :selected').text();
$('.selectedImage img').attr('src','../wp-content/themes/myTheme/styles/'+myImage+'/screenshot.jpg');
//if screenshot.jpg does not exist, use "../../default.jpg" instead
}
);

最佳答案

听起来您需要访问 HTTP 响应 header 。这是我在网上找到的一段代码。

来源:http://www.vfstech.com/?cat=1

$(document).ready(function() {
var ajaxSubmitOptions = {
// the normal success callback is not used
// success: function (responseText) {
// ...
// } ,
complete: function (xhrObj, status) {
if(status == "error") { // 500 server error?
alert("There was an error processing this request.");
} else {
if (xhrObj.getResponseHeader("X-My-Custom-Header") != "") {
alert("Intercepted special HTTP header...");
alert(xhrObj.getResponseHeader("X-My-Custom-Header"));
}
else {
// call the function you normally would have used in the "success" callback:
this._success(xhrObj.responseText);
}
}
} ,
_success: function (responseText) {
alert("Normal success callback...");
alert(responseText);
}
};

$('#myForm').ajaxForm(ajaxSubmitOptions);
})

关于jQuery 相当于 PHP 的 file_exists()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2490237/

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