我正在php codeignitor中使用AJAX上载一个图像,并且在上载时返回一个包含上载图像路径的JSON响应,然后我将该路径插入到一个img标记中进行预览
现在,我想做的是获得的高度和宽度呈现的img标签更新后,它的路径,但问题是,我总是得到一个0,所以请有人帮助我在这里
这是我的JS
function uploadAd(){ // console.log($('#uploadedPDF').prop('files')); var file_data = $('#uploadedAdFile').prop('files')[0]; var form_data = new FormData(); form_data.append('uploadedAdFile', file_data); // alert(form_data); $.ajax({ url: BASE_URL+'home/uploadAdFile', // point to server-side PHP script dataType: 'json', // what to expect back from the PHP script, if anything cache: false, contentType: false, processData: false, data: form_data, type: 'post', async: false, success: function(data){ console.log(data); if(data.status == -1){ // If file extension Validation failed $('#uploadedAdFileMsg').html(data.msg); $('#uploadedAdFileMsg').removeClass('hidden'); $('#uploadedAdFilePreview').addClass('hidden'); } else{ // If file extension Validation passed $('#uploadedAdFilePreview').attr('src', data.path); // Render file in img tag $('#uploadedAdFileMsg').addClass('hidden'); $('#uploadedAdFilePreview').removeClass('hidden'); console.log(document.getElementById('uploadedAdFilePreview').naturalWidth); // console.log() } }, complete: function(){ console.log('Complte '+document.getElementById('uploadedAdFilePreview').naturalWidth); } }).done(function(){ console.log('Done '+document.getElementById('uploadedAdFilePreview').naturalWidth); }); } $(document).ajaxComplete(function(){ console.log('AJAX '+document.getElementById('uploadedAdFilePreview').naturalWidth); })
正如你所看到的,我在不同的地方插入了得到的高度代码,但没有运气
PS:我也用了clientHeight,height,naturalHeight,但还是0
所以我得出的结论是height函数是在img标记呈现其内容之前调用的,因此它显示一个0
那么我可以在AJAX成功函数中包含onload类型的函数吗?
谢谢
我解决了:D,
实际上,我在Jquery返回的对象上使用了onload,这是错误的,它应该在document.getElement
上使用,所以我将
element.onload=function(){getHeight();}
在我的success函数os AJAX中,它工作了:)