首頁 > web前端 > js教程 > 主體

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

coldplay.xixi
發布: 2020-11-30 17:02:01
原創
2571 人瀏覽過

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

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

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

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

第一種:

$("") // Make in memory copy of image to avoid css issues  在内存中创建一个img标记
    .attr("src", $(''#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 = $(''#imgid").attr( "src"); 
alert( "Width: " + theImage.width); 
alert( "Height: " + theImage.height);
登入後複製

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

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

#

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!