Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.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/8/visual-studio-code/3.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图像加载的宽度和高度分别为0和0_Javascript_Html - Fatal编程技术网

javascript图像加载的宽度和高度分别为0和0

javascript图像加载的宽度和高度分别为0和0,javascript,html,Javascript,Html,我正在从文件系统加载图像,然后我想调整图像的大小。它在除IE(11)之外的所有浏览器中都可以正常工作。问题是图像已加载,但宽度和高度为0 我没有选择,所以我把这个问题贴在这里 这是密码 function getAsImage(readFile, chatboxtitle) { var reader = new FileReader(); reader.readAsDataURL(readFile); reader.onload = addImg; } function

我正在从文件系统加载图像,然后我想调整图像的大小。它在除IE(11)之外的所有浏览器中都可以正常工作。问题是图像已加载,但宽度和高度为0

我没有选择,所以我把这个问题贴在这里

这是密码

function getAsImage(readFile, chatboxtitle) {
    var reader = new FileReader();
    reader.readAsDataURL(readFile);
    reader.onload = addImg;

}

function addImg(imgsrc) {
    var img = document.createElement('img');
    img.setAttribute("src", imgsrc.target.result);

    console.log(img);

    console.log(img.width + " "+img.height);

    ... a lot of cool stuff happens here but it doesn't work because width is 0

}
控制台会打印图像,就像

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABAAAAAH0CAYAAAHUIW ... and much more.. >

所以图像就在那里。除IE外,所有浏览器都可以正常工作

我试着设置一些IMG属性,比如样式、宽度,但没有效果

注意,我不是一个硬核JavaScript开发人员。也许我错过了什么。找不到DOM元素吗

更新

我昨天尝试了下面的答案,结果成功了。但我今天尝试了这个解决方案,但它已经不起作用了。试试这个。
它在Windows7的IE11中不起作用,但在Windows8的IE11中起作用。

奇怪的IE行为。事实证明,您需要使用

img.src = src_goes_here
不通过
setAttribute

(function(doc){
    doc.getElementById('file').addEventListener('change', readFile);

    function readFile(ev) {
        var file = this.files[0],
            reader;

        if(file) {
            reader = new FileReader();
            reader.onload = getSize
            reader.readAsDataURL(file);
        }
    }

    function getSize(ev) {
        var img = doc.createElement('IMG');
        img.src = this.result; //wors in IE11
        //img.setAttribute('src', this.result); //doesn't work

        console.log(img.width, img.height);
    }
}(document))

UPD:一些额外的研究表明,为了让IE在使用
img.setAttribute
设置src时计算图像大小,您需要实际将图像附加到文档。比如说

document.body.appendChild(img);

您是否尝试过img.setAttribute(“样式”、“宽度:100px;高度:100px”)?似乎返回
img
width
height
有趣的是,我没有注意到图像没有附加到DOM,我认为如果不将其附加到DOM,就无法获得任何东西的尺寸。奇怪的是,如果设置节点的src属性,就可以得到。不是通过
setAttribute
方法。我试过了,它工作了,但现在不再工作了。看我加上的小提琴,仍然对我有用。重新加载浏览器或其他东西。好的,它在windows 8的IE11中工作,但在windows 7的IE11中不工作