【问题标题】:Classic Asp - JavaScript : Height and width is always zero in chrome经典 Asp - JavaScript:chrome 中的高度和宽度始终为零
【发布时间】:2019-03-18 07:43:58
【问题描述】:

我的标记中有一个图像字段。我正在使用 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
        }

请指教!

【问题讨论】:

  • 将您的 html/css 也显示为工作代码
  • 只是表格的宽度和高度
  • 尝试使用Image() 构造函数来获取尺寸。
  • 是的,我们已经使用 Image() 来获取尺寸,它在 IE 中有效,但在 Chrome 中无效,知道为什么它在 chrome 中无效。

标签: javascript html css asp-classic


【解决方案1】:

你试过了吗:

<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 等。您可以尝试将元素 id="YourElemId" 设置为 style="display:block;"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    相关资源
    最近更新 更多