无法使用 jquery 获取绝对位置元素的宽度
Cannot get the width of an absolute position element with jquery
请帮助我,我无法使用 jquery 获取绝对位置元素的宽度。我的元素使用:height: 100%
和 width: auto
通过 CSS。
我使用 $(element).width()
但获得了 0。
这是一个例子:http://codepen.io/anon/pen/LVjBvz
我需要元素采用内部图像的宽度。此图像有 4000x800 像素,但我在 css 中使用 width:100% 和 height:auto 使图像全屏显示
您的代码在图像更改为完全加载之前正在执行。使用$(window).load(
等待它完成:
$(window).load(function () {
var contimg;
contimg = $('#cont_img');
ancho = contimg.width();
alert(ancho);
})
参考:https://api.jquery.com/load-event/ and https://learn.jquery.com/using-jquery-core/document-ready/
The load event is sent to an element when it and all sub-elements have
been completely loaded. This event can be sent to any element
associated with a URL: images, scripts, frames, iframes, and the
window object.
请帮助我,我无法使用 jquery 获取绝对位置元素的宽度。我的元素使用:height: 100%
和 width: auto
通过 CSS。
我使用 $(element).width()
但获得了 0。
这是一个例子:http://codepen.io/anon/pen/LVjBvz
我需要元素采用内部图像的宽度。此图像有 4000x800 像素,但我在 css 中使用 width:100% 和 height:auto 使图像全屏显示
您的代码在图像更改为完全加载之前正在执行。使用$(window).load(
等待它完成:
$(window).load(function () {
var contimg;
contimg = $('#cont_img');
ancho = contimg.width();
alert(ancho);
})
参考:https://api.jquery.com/load-event/ and https://learn.jquery.com/using-jquery-core/document-ready/
The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.