Javascript 函数无法获取高度 css 属性

标签 javascript html css

我有以下站点:

http://www.pachamber.org/www/advocacy/index.php

当用户点击底部的“General Commerce”href 标签时,应该会滑出隐藏的内容。除此标签外,所有其他标签均正常工作。

该函数仅在 IE 中表现出意外。它在 Chrome 和 FF 中看起来没问题。调试函数的时候,好像没有抓不到height来自 div 的属性:

<div id="general" style="display: none; height: 30px; overflow: hidden">

height属性在此行显示为 1px:

this.height = parseInt(this.obj.style.height);

这是 HTML 代码片段和函数调用:

<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
    <td colspan="2" style="width: 100%;">
      <div class="subheading2"  style="border-bottom: thin solid gray; cursor: pointer; color: #000099" onClick="doSlideOut('general');"><a name="general"></a>General Commerce</div>
    </td>
</tr>
</table>
<div id="general" style="display: none; height: 30px; overflow: hidden">
<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
    <td width="53%">
        &bull; <a href="gc/testimony/index.php" >Testimony &amp; Comments</a>
    </td>
    <td width="47%">&nbsp;</td>
</tr>
</table>
</div>

知道我遗漏了什么吗?

谢谢。

最佳答案

Beware of id and name attribute when using getElementById in Internet Explorer 描述了导致您的问题的 IE 的愚蠢行为。

如果有两个元素的 idname 具有相同的值(在您的情况下,它是 id 为 general-commerce 和链接 General Commerce) IE 将在使用 getElementById 时获取其中一个。

解决方案是更改链接的名称属性或 div 容器的 ID。

关于Javascript 函数无法获取高度 css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10339772/

相关文章:

javascript - 从ajax请求返回一个对象

javascript - 使用 Eval 显示 chop 的字符串

html - 位置相对靠后的两个 div(重叠)

css - 平板电脑最小分辨率和最大分辨率的媒体查询

javascript - 无法禁用vue js Material 日期选择器中的日期

javascript - 带标签栏的温泉滑动菜单不起作用

php - JSSOR 与皮肤上的 jQuery 单图像问题 thumbnail-navigator-07

html - 如何从导航栏中的链接创建下拉菜单

javascript - 未设置时获取元素宽高

html - Navbar Burger(下拉)将内容向下推?