javascript - 经典 Asp - JavaScript:chrome 中的高度和宽度始终为零
问题描述
我的标记中有一个图像字段。我正在使用 javascript 设置其来源。我需要它的高度和宽度。为此,我使用了 image.height() 和 image.width() 方法。它在 Internet Explorer 中正常工作,但在 chrome 中不起作用,我尝试了 image.height 和 image.width 方法,但没有得到任何价值。我也尝试通过将 prop 方法更改为 attr 来设置图像 src,但它也给了我相同的结果。我在这里粘贴我的代码。
function new()
{
timg = new Image();
timg.src=fpath;
}
我用另一种方法称之为
function Size(timg) {
var con=150;
th=timg.height;
alert("th="+th); //in chrome always returns Zero
tw=timg.width;
alert("tw="+tw); //in chrome always returns Zero
}
请指教!
解决方案
你试试:
<script>
var tw = document.getElementById('YourElemId').clientWidth;
document.getElementById('result').innerHTML = tw;
</script>
<div id="result"></div>
浏览器兼容性。https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth
如果图像 id="YourElemId" 不成功,也许您可以使用 div id="YourElemId" 包装图像并执行 document.getElementById('YourElemId').clientWidth 。用于测试 div style="border:1px solid #000;max-width:?px;"
我读到 clientWidth 属性不适用于具有 display:inline? 的元素,默认情况下有几个元素内联,如 span、img 等。您可以尝试使用 style="display:block;" 设置元素 id="YourElemId"
推荐阅读
- java - 如何在实体之间创建 JPA 规范连接
- flutter - Flutter:架构 arm64 的未定义符号
- javascript - 没有选项进入动态填充的选择标签
- javascript - PostgreSQL:从 CSV 文件读取数据时出现 INSERT 错误
- performance - 如果来源太多,预连接来源的顺序是否重要
- angular - 当有待处理的请求时,我的应用程序卡住了
- c# - 20 倍的性能差异 Interlocked.Read 与 Interlocked.CompareExchange 虽然都使用 lock cmpxchg 实现
- python - python(烧瓶)中的给定模块错误中没有名称
- linux - 对于给定条件,使用 AWK 将一个文件中的列添加到另一个文件中
- linux - 如何解决无法解析“proxy.iiit.ac.in”