angularjs如何找到img元素的高度和宽度

时间:2013-12-16 02:08:45

标签: javascript jquery css angularjs angularjs-directive

您好我正在尝试查找图像元素的高度和宽度以调整其大小。但是每次我使用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

  }
  });

1 个答案:

答案 0 :(得分:2)

见下面的代码:

.directive('lightboxResizeImage', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
             console.log(elem.parent().width()); // =575
             console.log(elem[0].offsetHeight);
        }
    }
});