假設圖片的原始高度爲200像素或低於100像素。然後我設置圖片的「最大高度:100px」。我將如何獲得它的顯示高度。我試過$('img')。height()。但它只是返回給我一個0值。如何在javascript中獲取圖片的高度
0
A
回答
0
使用window.getComputedStyle
(火狐,歌劇,Safari瀏覽器):
例如:
<img style="height:100px;max-height:100px;" src="img.gif" id="test">
var img = document.getElementById('test');
var height = window.getComputedStyle(img).height;
console.log(height)
會輸出100像素
對於IE,你可以使用
var height = img.currentStyle.height;
0
使用簡單的JavaScript:
var img = document.getElementById('imageid'); // use the id of your image
var height = img.clientHeight;
0
你需要$(「IMG」)innerHeight() - 獲取第一個元素的當前計算高度匹配的元素,包括填充物而不是邊界。 。 http://api.jquery.com/innerHeight/
0
$(document).ready(function() {
$("img").load(function() {
alert($(this).height()); // for height
alert($(this).width()); // for width
});
});
0
請確保您已經在此腳本的上方初始化jQuery,因爲在這種情況下,您似乎試圖使用「$」命令使用jQuery。
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
將上面的代碼放在文檔的頭部。
在體內,但圖像
<body>
<img src="img.jpg" id="new-image" />
</body>
上面是HTML,圖像具有「新圖像」的一個ID
ID可以使用jQuery通過前添加#來選擇ID
eg 「#new-image」
$(document).ready(function(){
// Retrieves computed image height and stores to variable "imgHeight"
var imgHeight = $('#new-image').height();
// Shows current computed/displayed height in development console
console.log(imgHeight);
// If you don't know how to access the development console
// You can also use alert (but you should learn to use the console!)
alert(imgHeight);
}
確保將上述代碼包裝在腳本標記中。
相關問題
- 1. 如何使用Javascript獲取圖片的寬度和高度?
- 2. 無法在javascript中獲取圖片的高度和寬度
- 3. 在javascript中獲取圖像的高度
- 4. 如何獲取C#中圖片的高度和寬度
- 5. 如何在WooCommerce中獲取商店目錄圖片的高度
- 6. 如何在Silverlight中獲取圖片的高度?
- 7. 獲取圖片高度和寬度
- 8. 獲取圖像寬度高度的JavaScript
- 9. 如何在Android中獲取照片的寬度和高度?
- 10. 在Python中獲取輸入圖片的寬度和高度
- 11. 如何在django中獲取上傳圖片的寬度和高度?
- 12. 如何在JavaScript中獲取更新的高度和寬度?
- 13. Javascript:如何獲取循環中未加載圖像的高度?
- 14. 在javascript中獲取框架的高度
- 15. 在Javascript中獲取圖像變量的高度和寬度
- 16. Javascript獲取圖像的高度?
- 17. 如何獲得PHP中SVG圖片的寬度和高度?
- 18. 獲取片段視圖中組件的寬度和高度
- 19. 從MySQL中獲取圖片的高度和寬度db
- 20. 獲取不在內容中的圖片高度
- 21. 我如何改變從wordpress中獲取帖子圖片的寬度和高度?
- 22. Facebook圖形API iOS:如何獲取Feed圖片的寬度和高度?
- 23. 通過JavaScript獲取圖像高度
- 24. 如何在純JavaScript中獲取像素的高度?
- 25. 如何獲取存儲在JavaScript對象內的圖像的寬度和高度?
- 26. 如何在OpenCV中獲取圖像寬度和高度?
- 27. 如何在Android中獲取高度
- 28. 如何在firefox中獲取iframes高度
- 29. 如何從AVFrame獲取亮度圖片?
- 30. 用php獲取圖片的高度和寬度
請發佈您的htmn代碼和js代碼。提前致謝。 – Christos
爲什麼你不試圖獲得最大高度呢? '$('#yourImage')。css('max-height');' – Alvaro
如果圖片的高度低於其最大高度怎麼辦?我不想返回最大高度本身。 – ILikebanana