4

使用 onload 给改边图片宽高无效

 2 years ago
source link: https://www.v2ex.com/t/799227
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  JavaScript

使用 onload 给改边图片宽高无效

  aglsv · 8 小时 46 分钟前 · 617 次点击

async function setLatexSize(arr_latexImg, fontSize) {
        for (let i = 0;i< arr_latexImg.length;i++){
            let newImg = new Image()
            newImg.src = arr_latexImg[i].src
            await new Promise((resolve) => {
                newImg.onload = function() {
                    arr_latexImg[i].height = fontSize * newImg.height / 16
                    arr_latexImg[i].width = fontSize * newImg.width / 16
                    arr_latexImg[i].setAttribute('width', fontSize * newImg.width / 16);
                    arr_latexImg[i].setAttribute('height', fontSize * newImg.height / 16);
                    resolve()
                }
            })
        }
    }

我想跟踪字号的改变来设置图片的宽高,通过 for 循环遍历每一个 img 元素,之后通过 onload 里面来设置 img 元素的宽高,但是现在的问题是当我 console 出arr_latexImg[i]的时候,他标签上面是有宽高属性的,但是我页面中的元素本身依旧是原来的样子,没有变化

img

img

上面是页面中的元素标签,下面的图是我在 console 中显示的arr_latexImg[0]arr_latexImg[0]中的属性是设置上了,但是元素本身依旧没有, arr_latexImg这个数组是通过 jq 获取元素,完了 each 一个新的数组,r 这个数组就是给我传的arr_latexImg,下面是压缩后的代码。我想知道目前的问题是在哪块,是我方法哪块写的有问题吗

t = $("div.table").children().find("img.hex-exe-img"),
                        r = [];
                        $.each(t, function(e, n) {
                            r.push(t[e])
                        }),



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK