javascript angularjs 如何找到 img 元素的高度和宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20602382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:51:32 来源:igfitidea点击:
angularjs how to find img element height and width
提问by user1424508
Hi I'm trying to find an image element's height and width in order to resize it. However everytime I use elem.height() or elem.width() I get 0?
嗨,我正在尝试查找图像元素的高度和宽度以调整其大小。但是,每次我使用 elem.height() 或 elem.width() 时我都会得到 0?
html
html
<div>
<img data-ng-src="{{photosrc}}" lightbox-resize-image>
</div>
directive.js
指令.js
.directive('lightboxResizeImage', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
console.log(elem.parent().width()); //==equals 575
console.log(elem.width()); //this always equals 0
}
});
回答by trinhvanhuy
See code below:
见下面的代码:
.directive('lightboxResizeImage', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
console.log(elem.parent().width()); // =575
console.log(elem[0].offsetHeight);
}
}
});