gpt4 book ai didi

javascript - jquery 未获取服务器上的图像值

转载 作者:行者123 更新时间:2023-12-03 03:26:57 24 4
gpt4 key购买 nike

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

尝试将代码嵌套在 jquery 中,但它确实在图像完全加载之前触发,所以我相信这就是问题所在。

这是 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 将等待图像被加载,这样你就可以获取它们的尺寸。

关于javascript - jquery 未获取服务器上的图像值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46281312/

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