我的元素的 data-attr 中有原始图像 src。我想要得到这张图片的高度。但我不想将图像附加到 DOM。我怎样才能得到高度?我的代码:
const imgSrc = $target
.closest('.js-photo-item')
.find('.photos__img--value')
.data('original-image');
const $img = $(new Image()).attr('src', imgSrc);
console.log($img.height()) // this return 0
最佳答案
const imgSrc = $target
.closest('.js-photo-item')
.find('.photos__img--value')
.data('original-image');
const $img = $(new Image()).attr('src', imgSrc);
$img.on('load', () => {
console.log($img[0].height) // return height img
});
感谢 George P
用户。他的回答在上面......
关于javascript - 如何在 DOM 中不附加此图像的情况下获取 img 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54343047/