在我的Wordpress中,我将自定义附件大小设置为128x79和896x277。我的图像将肯定大于128x79。如果图像小于896x277,它将截断大于最大尺寸的部分,并将另一个设置为最大尺寸。这意味着500x300的图像将被裁剪为500x277。
假设在我的页面上,我有一个图像与href http://domain.com/files-128x79.jpg
,我想用files-896x277.jpg
替换files-128x79.jpg
,如果它存在,否则,找到最大的图像与高度作为优先级。
好吧…我决定简化我的问题,使它更简单。我现在要做的就是检查files-896x277.jpg是否存在,如果不存在,我希望它输出files.jpg。到目前为止,我已经得到了这个,但似乎无法设置锚。
$rewrite.find('a').each(function(index, element) {
$.ajax({
url : $(this).children().attr('src').replace('-128x79.', '-896x277.'),
type: 'head',
success : function() {
$(this).attr('href', $(this).children().attr('src').replace('-128x79.', '-896x277.'));
},
error : function(xhr, d, e) {
if (xhr.status == 404) {
$(this).attr('href', $(this).children().attr('src').replace('-128x79.', '.'));
}
}
});
});
我做错了什么?
在AJAX回调,什么是在this
?它仍然是导致事件的因素吗?如果有,是哪个事件?AJAX结果事件?
我建议将this
保存到一个本地变量,以确保它包含您期望的内容:
$(function () {
var that = $(this);
$.ajax({
url : that.children()....
是的,你可以这样做:
var image = 'files-128x79.jpg';
$(function () {
$.ajax({
url : 'search_images.php',
success : function(filelist) {
//YOu have the filelist in a string. Just parse it to find the image
...
var imageExists = (filelist.indexOf(image) >= 0); //Search the image
if (!imageExists ) {
alert('The image was not found');
}
},
});
});
PHP: search_images.php (linux)
echo `ls /images/files-*.jpg`; //This will list all the images in your directory
希望这对你有帮助。欢呼声