如何以像素为单位获得Div或表格的宽度?

时间:2022-11-19 13:52:59

The main reason of this question is because I need the Width of a DIV element which holds an image, but the resize script (timtumb) only accepts pixel size and not % (will be great). for example :

这个问题的主要原因是因为我需要一个包含图像的DIV元素的宽度,但是调整大小脚本(timtumb)只接受像素大小而不是%(会很棒)。例如 :

 timthumb.php?src=castle1.jpg&h=230&w=640 
If I set the width 640 I will have the image size to be displayed on PC will be Ok, but this same page will need to be shown on tablet or cellphone and the width on these devices are narrow, for example 480px, so the DIV can be adjusted to 480px, but the scrip line need to change to:

 timthumb.php?src=castle1.jpg&h=125&w=480 

this script only accepts numbers and not % or px
I can do a multiple line script and hide some of these DIV's with CSS as responsive, but I want to enhance this code script :) if it possible, if not I will do a responsive patch.

这个脚本只接受数字而不是%或px我可以做一个多行脚本并用CSS作为响应来隐藏这些DIV中的一些,但我想增强这个代码脚本:)如果有可能,如果不是,我会做一个响应补丁。

2 个解决方案

#1


0  

You can get div width px with javascript, and send it to php (via ajax for eg.)

您可以使用javascript获取div宽度px,并将其发送到php(例如通过ajax)。

<script type="text/javascript">
    var divWidth = document.getElementById("mydiv").clientWidth; 
</script>

#2


0  

Try

<div><img src="timthumb.php?src=castle1.jpg&h=230&w=640" style="width:100%;max-width:640px;" ></div>

Your image should "fit" automaticly to width of your parent div

您的图像应自动“适合”父div的宽度

#1


0  

You can get div width px with javascript, and send it to php (via ajax for eg.)

您可以使用javascript获取div宽度px,并将其发送到php(例如通过ajax)。

<script type="text/javascript">
    var divWidth = document.getElementById("mydiv").clientWidth; 
</script>

#2


0  

Try

<div><img src="timthumb.php?src=castle1.jpg&h=230&w=640" style="width:100%;max-width:640px;" ></div>

Your image should "fit" automaticly to width of your parent div

您的图像应自动“适合”父div的宽度