Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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 如何检索延迟图像的宽度和高度?_Javascript_Jquery - Fatal编程技术网

Javascript 如何检索延迟图像的宽度和高度?

Javascript 如何检索延迟图像的宽度和高度?,javascript,jquery,Javascript,Jquery,我需要得到的宽度和高度的图像是从一个API加载,没有css将被应用到图像。根据API中加载的内容,图像可能具有可变大小/ 目前使用jquery或vanilla js时,宽度和高度始终为0 我的代码中有什么问题?在创建img标签时,是否应该动态设置宽度和高度?怎么做 this.getItemSize = function() { var elm = $('#' + scope.elmItem); var pos = elm.offset();

我需要得到的宽度和高度的图像是从一个API加载,没有css将被应用到图像。根据API中加载的内容,图像可能具有可变大小/

目前使用jquery或vanilla js时,宽度和高度始终为0

我的代码中有什么问题?在创建img标签时,是否应该动态设置宽度和高度?怎么做

   this.getItemSize = function() {
        var elm = $('#' + scope.elmItem);
        var pos = elm.offset();

        /* also with this vanila js does not work
        var image = document.getElementById(scope.elmItem);
        console.log(image);
        var width = image.offsetWidth;
        var height = image.offsetHeight;
        */

        scope.itemTop = pos.top;
        scope.itemLeft = pos.left;
        scope.itemWidth = elm.width();
        scope.itemHeight = elm.height();
        console.log(scope.itemTop + " " + scope.itemLeft + " " + scope.itemWidth + " " + scope.itemHeight);
    };



            $.get(this.url, function(image) {
                // add img to the dom
                var img = $('<img id="item">');
                img.attr('src', scope.url);
                img.appendTo('#' + scope.elm);

                scope.getItemSize();
            });
this.getItemSize=function(){
var elm=$(“#”+scope.elmItem);
var pos=elm.offset();
/*同样,这个vanila js也不起作用
var image=document.getElementById(scope.elmItem);
console.log(图像);
变量宽度=image.offsetWidth;
var height=image.offsetHeight;
*/
scope.itemTop=位置top;
scope.itemLeft=位置左侧;
scope.itemWidth=elm.width();
scope.itemHeight=elm.height();
console.log(scope.itemTop+“”+scope.itemleeft+“”+scope.itemWidth+“”+scope.itemHeight);
};
$.get(this.url,函数(图像){
//将img添加到dom中

var img=$('加载映像后需要调用getItemSize,可以使用事件处理程序来执行此操作

$.get(this.url, function(image) {
    // add img to the dom
    var img = $('<img id="item">');
    img.load(function(){
        scope.getItemSize();
    })
    img.attr('src', scope.url);
    img.appendTo('#' + scope.elm);
$.get(this.url,函数(图像){
//将img添加到dom中

var img=$('Thank Alex for your edit:-)感谢它的工作,只是为了澄清一下,加载jquery方法是吗?@GibboK是的,它是由
img
触发的事件,jquery使用
load
方法向元素注册加载事件回调