gpt4 book ai didi

Javascript document.getElementById 似乎不起作用

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

我有以下代码,chrome 中的 javascript 控制台显示“无法读取 null 的 innerHTML 属性。为什么 document.getElementById('display') 空手而归?

<!DOCTYPE html>
<html>
<head>
<title>Chat 3</title>
<script type="text/javascript">
function showmsg(str){
var display = document.getElementById('display');
display.innerHTML += "<p>" + str + "</p>";
}

if("WebSockets" in window){
pass;
}else{
showmsg("Your browser doesn't support WebSockets. Try Google Chrome.");
}
</script>

<style type="text/css">
#username {
padding: 0px;
margin: 0px;
height: 26px;
width: 400px;
}

/* ADDED container div that wraps onlineusers and display */
#container {
margin: 10px 0;
}

/* use float: left to put them side-by-side */
#display {
padding: 0px;
margin: 0px;
border-style: solid;
border-width: 1px;
overflow: auto;
height: 400px;
width: 400px;
float: left;
}

#onlineusers {
padding: 0px;
margin: 0px;
height: 400px;
width: 200px;
border-style: solid;
border-width: 1px;
float: left;
}

/* Added container2 to wrap inputline and sendbutton */
#container2 {
margin: 10px 0;
}

#inputline {
padding: 0px;
margin: 0px;
height: 26px;
width: 350px;
float: left;
}

#sendbutton {
padding: 0px;
margin: 0px;
height: 30px;
width: 50px;
float: left;
}

/* this is a well used "hack". */
.clearfix {
clear: both;
}
</style>
</head>
<body onload="document.getElementById("username").focus()">
<input type="text" id="username" />
<div id="container">
<div id="display"></div>
<div id="onlineusers"></div>
<div class="clearfix"></div>
</div>

<div id="container2">
<input type="text" id="inputline" length="55" />
<input type="button" id="sendbutton" value="Send" />
</div>
</body>
</html>

最佳答案

将您的脚本部分向下移动到页面底部。否则它会在页面加载之前执行。

顺便说一句:将脚本放在页面底部甚至是 Google 推荐的“最佳实践”,Yahoo &公司

关于Javascript document.getElementById 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3210060/

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