Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery未在服务器上获取映像值_Javascript_Jquery - Fatal编程技术网

Javascript jquery未在服务器上获取映像值

Javascript jquery未在服务器上获取映像值,javascript,jquery,Javascript,Jquery,我有一个javascript,它应该得到图像的实际宽度和高度。当我在快照上的127.0.0.1:8000控制台日志上进行本地测试时,它可以工作,但在服务器上失败时,它返回0作为图像宽度和高度 尝试将代码嵌套在jquery ready中,但它确实会在映像完全加载之前触发,所以我认为这就是问题所在 下面是javascript $(function() { $(document).ready(function() { $('.lynk-section img').each(

我有一个javascript,它应该得到图像的实际宽度和高度。当我在快照上的127.0.0.1:8000控制台日志上进行本地测试时,它可以工作,但在服务器上失败时,它返回0作为图像宽度和高度

尝试将代码嵌套在jquery ready中,但它确实会在映像完全加载之前触发,所以我认为这就是问题所在

下面是javascript

$(function() { 
    $(document).ready(function() { 
        $('.lynk-section img').each(function() {
            var width = $(this)[0].naturalWidth;
            console.log(width);
            //$(this).css('max-width', width+'px');

            var height = $(this)[0].naturalHeight;
            console.log(height);
            //$(this).css('max-height', height+'px');
        });
    });

});

$(function(){
$(document).ready(function(){
是同一件事(第一件是速记),您只需要一个。但无论如何,这将只等待所有HTML代码出现在页面中,而不是等待加载资产。正如@Rory所说,
window.load
将等待加载图像,因此您可以获取图像的尺寸。

您必须使用图像的onload事件,或者您可能只需要使用event

/。。。。
$('img')。每个(函数(){
$(this).on(“加载”,函数(){
log(“宽度:”,this.naturalWidth);
console.log(“高度:”,这是自然高度);
});
});
//..

将代码置于窗口加载事件下

$(function() { 
    $(document).on('load',function() {
        // Your code to measure image dimensions should go here.
    });
});

窗口下运行您的代码。加载
这是答案是的,尽管这不是重复的,但我强烈建议您阅读有关
$(文档)之间差异的问题。准备好了
onload
,您可能会发现它很有用仅供参考,
每个()
在这里是多余的。你需要
窗口。加载
事件是正确的,但是你的代码示例没有做到这一点。@Rorymcrossan,我刚刚给了他重要的线索。他必须在我提到的注释处编写代码。“线索”不是答案,而将代码放在注释所在的位置也没有帮助。你已经附加了加载项dler到文档,而不是窗口,这是重要的区别。加载窗口后,您需要运行代码,因为只有这样才能在JS中读取图像尺寸。是的,@Rorymcrossan,您是对的。我正在向您学习。:-)