我有从php加载动态图像的页面。图像高度为各种PX大小(70,130,100,60).我的需求是如何在警报框中自动获取当前在页面中查看的图像大小,我尝试相同的代码不起作用。
法典
<?php
foreach ($images as $image) {
?>
<img src="<?php echo $image['url']; ?>" class="img_test" id="<?php echo $image['url']; ?>" width="70" style="max-height:70px" onclick="imagesize(this)" />
<?php
}?>
<script>
$( window ).load(function() {
$( ".img_test" ).each(function( index ) {
alert('Height: '+$(this).height());
});
});
function imagesize(obj){
alert($(obj).height());
}
</script>
如果我们单击图像,则高度将在自动无法显示后 display.in 页面加载
重要的是要了解它是导航器(客户端),它加载图像和PHP运行到您的服务器并将文本发送到客户端。
使用侦听器.on
获取事件"单击"。
此外,使用 $(document).ready
而不是 .load 来等待图像加载
<?php foreach ($images as $image) { ?>
<img src="<?php echo $image['url']; ?>" class="img_test" id="<?php echo $image['url']; ?>" width="70" style="max-height:70px" />
<?php } ?>
$(document).ready(function(){
$( ".img_test" ).each(function(){
alert('Height: '+$(this).height());
});
$(".img_test").on("click", function(){
alert($(this).height());
});
});
http://jsfiddle.net/3c9CN/