【问题标题】:Get width and height of image on mouse click点击鼠标获取图片的宽度和高度
【发布时间】:2020-03-23 18:45:50
【问题描述】:

我想在点击时获取图像的宽度和高度。以下是我的代码:

$(".sim-row-edit-img").click(function(){
	alert("Image clicked, width is __px and height is __px");

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<img class="sim-row-edit-img" border="0" alt="W3Schools" src="http://icons.iconarchive.com/icons/dtafalonso/android-l/256/WhatsApp-icon.png" width="100" height="100">

我想要图像的尺寸,而不是我在img attr 中写的width="100" height="100" 标签。

【问题讨论】:

标签: javascript jquery html image


【解决方案1】:

您应该将参数传递给回调函数,然后访问事件的target 及其naturalWidthnaturalHeight 属性,如下所示:

$(".sim-row-edit-img").click(function(e){
	alert("Image clicked, width is "+e.target.naturalWidth+"px and height is "+e.target.naturalHeight+"px");

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<img class="sim-row-edit-img" border="0" alt="W3Schools" src="http://icons.iconarchive.com/icons/dtafalonso/android-l/256/WhatsApp-icon.png" width="100" height="100">

【讨论】:

    猜你喜欢
    • 2017-03-19
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2011-11-26
    相关资源
    最近更新 更多