我有圖像標記以下如何從圖像標籤獲取寬度和高度屬性?
<img class="someclass" src="somesrc" width="220" height="165" />
的瀏覽器,其原有的寬度和高度,這是480x360
顯示圖像我想要得到的圖像寬度和高度從圖像標籤的屬性即應返回220 and 165
我曾嘗試以下
var img = document.getElementsByClassName('someclass');
console.log(img.clientWidth);
console.log($('.someclass').attr('width'));
但它返回我undefined
。
以下代碼返回圖像的實際寬度和高度。這是480
$('.someclass').width();
有什麼辦法,它應該返回220 and 165
?
'img'是'HTMLCollection',而不是元素 – MysterX
'document.getElementsByClassName( 'SomeClass的')[0]'' –
$(」。SomeClass的 ')。ATTR(' 寬度 ')'應當' ve worked fine – adeneo