gpt4 book ai didi

javascript - 如何在 Angular 2 中获取元素的宽度或高度?

转载 作者:太空宇宙 更新时间:2023-11-04 15:42:26 25 4
gpt4 key购买 nike

图像加载后无法获取实际宽度或高度。有谁知道如何以正确的方式做到这一点?

HTML:

<div class="editor-container">
<img src="{{photoUrl}}" alt="" />
</div>

CSS:

.editor-container {
position: relative;
width: 100%;
}

.editor-container img {
display: block;
width: 100%;
height: auto;
}

typescript :

ngAfterViewInit(){
try {
console.log( document.getElementsByClassName('editor-container') );
console.log( document.getElementsByClassName('editor-container')[0] );
console.log( 'clientWidth', document.getElementsByClassName('editor-container')[0].clientWidth );
console.log( 'scrollWidth', document.getElementsByClassName('editor-container')[0].scrollWidth );
} catch (err) {
console.error(err);
}
}

结果:

the console log image

最佳答案

问题解决了,因为div限制了图片的大小。

CSS

.editor-container {
position: relative;
}

.editor-container img {
display: block;
height: auto;
}

关于javascript - 如何在 Angular 2 中获取元素的宽度或高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com