首頁  >  文章  >  web前端  >  jquery中如何取得圖片真實大小

jquery中如何取得圖片真實大小

coldplay.xixi
coldplay.xixi原創
2020-11-18 11:14:132693瀏覽

jquery中取得圖片真實大小的方法:1、方法一【.attr("src", $(''#imgid").attr("src"))】;2、方法二【 theImage.src = $(''#imgid").attr( "src");】。

jquery中如何取得圖片真實大小

本教學操作環境:windows10系統、jquery2.2.4,本文適用於所有品牌的電腦。

jquery中取得圖片真實大小的方法:

第一種:

$("<img/>") // Make in memory copy of image to avoid css issues  在内存中创建一个img标记
    .attr("src", $(&#39;&#39;#imgid").attr("src"))
    .load(function() {
        pic_real_width = this.width;   // Note: $(this).width() will not
        pic_real_height = this.height; // work for in memory images.
    });

第二種:

var theImage = new Image(); 
theImage.src = $(&#39;&#39;#imgid").attr( "src"); 
alert( "Width: " + theImage.width); 
alert( "Height: " + theImage.height);

 第一種要安全性要高些,保證是在圖片載入完成後在取得。

相關免費學習推薦:JavaScript(影片)

#

以上是jquery中如何取得圖片真實大小的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn