gpt4 book ai didi

javascript - document.getElementById(仍然)不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:33 28 4
gpt4 key购买 nike

我在这里发起了一个话题document.getElementById not working但看起来即使提出的建议都是有效的,我仍然有问题。

我有几个复选框。当我在这里查看页面源代码时,有。 document.getElementById('chk1') 是唯一一个不为空的。怎么可能?

<input id="chk0" value="JobStages###StageCode~JobCode###DRAW~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;"  type="checkbox" />

<input id="chk1" value="JobStages###StageCode~JobCode###FEAS~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;" type="checkbox" />

<input id="chk2" value="JobStages###StageCode~JobCode###N/C~1005" onclick="addRemoveRow(this.value,this.checked)" style="border-width:0px;padding:1px;margin:0px;height:14px;" type="checkbox" />

编辑:几乎完整的代码

<tr id='rw1' onMouseOver='setHover(this,event);' class='DRAW~1005      '
onClick='setClick(this,event);'>
<td id='RowId_0' width=0 style='display: none'>1</td>

<td id='PrimaryKey_0' width=0 style='display: none'>DRAW~1005</td>
<td id='StageCode_0' width=0 style='display: none'>DRAW</td>
<td id='Allocated_0' nowrap
style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: center; padding-left: 5px;'
class='col1'><input id="chk0"
value="JobStages###StageCode~JobCode###DRAW~1005"
onclick="addRemoveRow(this.value,this.checked)"
style="border-width: 0px; padding: 1px; margin: 0px; height: 14px;"
type="checkbox" /></td>
<td id='StageCode_0' nowrap
style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col2'></td>
<td id='StageDescription_0' nowrap
style='overflow: hidden; height: 16px; width: 123px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col3'
onclick="showTextEditor(event,'JobStages','StageDescription','StageCode~JobCode','DRAW~1005 ');">
</td>
<td id='StartDate_0' nowrap
style='overflow: hidden; height: 16px; width: 171px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col4'
onclick="showCalendar(event,'JobStages','StartDate','StageCode~JobCode','DRAW~1005 ');">
</td>
<td id='EndDate_0' nowrap
style='overflow: hidden; height: 16px; width: 112px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col5'
onclick="showCalendar(event,'JobStages','EndDate','StageCode~JobCode','DRAW~1005 ');">
</td>
</tr>

<tr id='rw2' onMouseOver='setHover(this,event);' class='FEAS~1005 '
onClick='setClick(this,event);'>
<td id='RowId_1' width=0 style='display: none'>2</td>

<td id='PrimaryKey_1' width=0 style='display: none'>FEAS~1005</td>
<td id='StageCode_1' width=0 style='display: none'>FEAS</td>
<td id='Allocated_1' nowrap
style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: center; padding-left: 5px;'
class='col1'><input id="chk1"
value="JobStages###StageCode~JobCode###FEAS~1005"
onclick="addRemoveRow(this.value,this.checked)"
style="border-width: 0px; padding: 1px; margin: 0px; height: 14px;"
type="checkbox" /></td>
<td id='StageCode_1' nowrap
style='overflow: hidden; height: 16px; width: 136px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col2'></td>
<td id='StageDescription_1' nowrap
style='overflow: hidden; height: 16px; width: 123px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col3'
onclick="showTextEditor(event,'JobStages','StageDescription','StageCode~JobCode','FEAS~1005 ');">
</td>
<td id='StartDate_1' nowrap
style='overflow: hidden; height: 16px; width: 171px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col4'
onclick="showCalendar(event,'JobStages','StartDate','StageCode~JobCode','FEAS~1005 ');">
</td>
<td id='EndDate_1' nowrap
style='overflow: hidden; height: 16px; width: 112px; overflow: hidden; text-align: left; padding-left: 5px;'
class='col5'
onclick="showCalendar(event,'JobStages','EndDate','StageCode~JobCode','FEAS~1005 ');">
</td>
</tr>

最佳答案

您可能在 DOM 节点可用之前执行 javascript。

这行不通

<script type="text/javascript">
// trying to retrieve #chk1 before it exists in the DOM!
document.getElementById('chk1');
</script>

<input id="chk1">

而其中任何一个都会

<input id="chk1">

<script type="text/javascript">
// #chk1 exists in DOM, this works
document.getElementById('chk1');
</script>

<script type="text/javascript">
// Force execution to wait until window is done i.e, DOM is ready
window.onload = function()
{
document.getElementById('chk1');
}
</script>

<input id="chk1">

编辑

我从上面复制了你的整个 HTML block ,并将其添加到它的末尾

<script type="text/javascript">
alert([
document.getElementById('chk0')
, document.getElementById('chk1')
]);
</script>

用IE7打开。警报产生了 [object],[object] 这意味着它有效并找到了两个节点。不知道该告诉你什么。

关于javascript - document.getElementById(仍然)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1266834/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com