无法通过javascript找到div [英] Can not find div by javascript
问题描述
大家好.
我有一个JavaScript函数,可在运行时调整列的height(div).
抛出异常,表明需要对象.
以下是我的功能...
Hi all.
I have a javascript function which adjusts the column height(div) at runtime.
An exception is thrown saying object required.
Following is my function...
function AdjustColumnsHeight() {
// get a reference to the two DIVS that make up the columns
var centerCol = window.document.getElementById(''centercol'');
var leftCol = window.document.getElementById(''leftcol'');
// calculate the max height
var hCenterCol = centerCol.offsetHeight;
var hLeftCol = leftCol.offsetHeight;
var maxHeight = Math.max(hCenterCol, hLeftCol);
// set the height of all 2 DIVS to the max height
centerCol.style.height = maxHeight + ''px'';
leftCol.style.height = maxHeight + ''px'';
}
以下是我的母版页.
And Following is my Master Page..
<div id="container">
<div id="leftcol">
<leftMenu:Leftmenue ID="leftMenu1" runat="server" />
</div>
<div id="container2">
<div id="centercol">
<asp:ContentPlaceHolder id="MainContent" runat="server"> /asp:ContentPlaceHolder>
</div>
</div>
</div>
如果我将centercol div放在首位,那么将找不到leftcol并且
如果我先把leftcol div放,那么就找不到centercol
请帮我,我很困惑.
If I put the centercol div first,then leftcol cannot be found and
If I put the leftcol div first,then centercolcannot be found
Please help me I am in puzzled
推荐答案
我试图按照您所说的那样从window.onload调用.如果div位于表单内,则可以正常工作
I tried to call from window.onload as you said.Works fine if div is inside form
<form id="form1" runat="server"> </form>
看起来您错误地放置了div,这可能会导致问题.将其放置在表单标签&中;再次测试.
Looks like you have placed div wrongly which might be causing the issue.Place in form tag & test again.
使用FF的FireBug和IE的IEWebDeveloper工具查找控件ID.因为如果内容占位符asp.net中的控件在控件ID前面放了一些前缀,例如ctl00_
.我建议,如果您不熟悉控件ID,那么可以使用上面提到的一些工具来找到控件ID.
use FireBug for FF and IEWebDeveloper tool for IE to find control ID. because if the control in content placeholder asp.net put some prefix to control id like ctl00_
. I suggest if you do not know the control id better to find control id using some tools I mentioned above.
这篇关于无法通过javascript找到div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!