Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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/2/jquery/82.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/0/search/2.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,我需要发送一个乘数到我的服务器,这样我就可以正确地管理我的形象。客户端可以上传图像,而onload我想立即找出自然宽度和客户端宽度之间的差异。图像位于350px的容器中,图像设置为minwidth:100%我在下面试过,奇怪的是,它成功了好几次 当我运行这个时,我得到了一个正确的自然宽度,但是clientWidth是0 下面是来自jcrop的一个方法。以及一些附加功能,以获得自然宽度和客户端宽度 $('#target').Jcrop({ onChange : update

我需要发送一个乘数到我的服务器,这样我就可以正确地管理我的形象。客户端可以上传图像,而onload我想立即找出自然宽度和客户端宽度之间的差异。图像位于350px的容器中,图像设置为
minwidth:100%我在下面试过,奇怪的是,它成功了好几次

当我运行这个时,我得到了一个正确的自然宽度,但是clientWidth是0

下面是来自jcrop的一个方法。以及一些附加功能,以获得自然宽度和客户端宽度

$('#target').Jcrop({
            onChange : updatePreview,
            onSelect : updatePreview,
            aspectRatio : xsize / ysize
        }, function() {
            // Use the API to get the real image size
            var bounds = this.getBounds();
            boundx = bounds[0];
            boundy = bounds[1];
            // Store the API in the jcrop_api variable
            jcrop_api = this;

            // Move the preview into the jcrop container for css positioning
            $preview.appendTo(jcrop_api.ui.holder);
        });

        function updatePreview(c) {

            $('#xmargin').val(Math.round(c.x));
            $('#ymargin').val(Math.round(c.y));
            $('#x2').val(Math.round(c.w));
            $('#y2').val(Math.round(c.h));

            var clientW = document.getElementById("target").clientWidth;
            var naturalW = document.getElementById("target").naturalWidth; 

            var multiply = naturalW/clientW;

            $('#multiply1').val(clientW); //Returns 0
            $('#multiply2').val(naturalW); //Returns correct value

            if (parseInt(c.w) > 0) {
                var rx = xsize / c.w;
                var ry = ysize / c.h;

                $pimg.css({
                    width : Math.round(rx * boundx) + 'px',
                    height : Math.round(ry * boundy) + 'px',
                    marginLeft : '-' + Math.round(rx * c.x) + 'px',
                    marginTop : '-' + Math.round(ry * c.y) + 'px'
                });
            }
        }

请参阅相关问题:javascript似乎在图像呈现之前触发;因此它的宽度仍然是0。感谢您的回复,但他们都没有提供任何我可以使用的信息。问题是我多次运行这个方法,所以img应该已经准备好了。那么您是否尝试过在
$(window.load)(function(){})
调用中调用它?