您好我正在尝试查找图像元素的高度和宽度以调整其大小。但是每次我使用elem.height()或elem.width()我都会得到0?
HTML
<div>
<img data-ng-src="{{photosrc}}" lightbox-resize-image>
</div>
directive.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
}
});
答案 0 :(得分:2)
见下面的代码:
.directive('lightboxResizeImage', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
console.log(elem.parent().width()); // =575
console.log(elem[0].offsetHeight);
}
}
});