TypeError:document.getElementById(...)为null.不知道为什么我收到这个错误.在Firebug中正常工作.
function init() {
updateClock ();
};
function updateClock ( )
{
var currentTime = new Date ( );
var currentHours = currentTime.getHours ( );
var currentMinutes = currentTime.getMinutes ( );
var currentSeconds = currentTime.getSeconds ( );
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
// Choose either "AM" or "PM" …Run Code Online (Sandbox Code Playgroud) 我是jQuery和学习的新手.由于一些奇怪的原因,以下代码不起作用(我正在使用Coda2):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Page</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$("#click").click(function () {
$("p").slideToggle("slow");
});
</script>
</head>
<body>
<p>Choose Your Language</p>
<button id="click">Click Me</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
任何帮助
一个document.getElementById回归的案例null.我在SO中读过其他四个问题,并阅读MDN上的参考资料,但我不知道出了什么问题; 请帮我.代码如下:
HTML
<button id="btnButton1">Button1!</button><br>
<button id="btnButton2">Button2!</button><br>
<span id="spanOutPut"></span>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
getBYid = function(elem) {
return document.getElementById(elem); }
funButton1 = function() { getBYid('spanOutPut').innerHTML = "Button 1 pressed!!" };
funButton2 = function() { getBYid('spanOutPut').innerHTML = "Did you press the Button 2?!" };
getBYid("btnButton1").addEventListener('click', funButton1, false);
getBYid("btnButton2").addEventListener('click', funButton2, false);
Run Code Online (Sandbox Code Playgroud)
我TypeError: getBYid(...) is null在FireBug上得到了一个.
当我只是addEventListener从JS中删除调用并设置onclick内联时,它就可以工作,如下面的代码所示:
<button onclick="funButton1()">Button1"</button>
Run Code Online (Sandbox Code Playgroud)
有什么不同?
我有这段代码.它得到一个错误document.getElementById('foo')为null.请帮助解决这个问题.
<script>
function doMove() {
var foo=document.getElementById('foo');
foo.style.left = (foo.style.left+10)+'px'; // pseudo-property code: Move right by 10px
setTimeout(doMove(),20); // call doMove() in 20 msec
}
doMove();
</script>
</head>
<body>
<div id="foo" style="width:10px;">a</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
<html>
<head>
<title>Demo</title>
<script>
function show()
{
var content = document.getElementById("content");
var sub = content.getElementById("sub1");
alert(sub.nodeName);
}
</script>
</head>
<body>
<div id="content">
<div id="sub1">
Content 1
</div>
<div id="sub2">
Content 2
</div>
<input type="button" value="Click" onclick="show()" />
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为什么content.getElementById和alert函数不起作用?谢谢你的回答.
我是jQuery的新手,我试图创建一个登录表单,当用户输入一个简短的用户名时切换文本.所以这是我的代码,当我点击按钮时没有任何反应.我检查了控制台,它说在我的code.I've特定的行标题在错误可能的拼写错误检查过,但我没有看到任何.所以这是我的代码:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src = "jquery-2.1.1.min.js"></script>
<script type="text/javascript">
var usr = document.getElementById("textbox1").value;//error is over this line.
if(usr.length<=6){
$(document).ready(function(){
$("#press").click(function(){
$("#warning").toggle();
});
});
}
</script>
</head>
<body>
<div id = "middleBlock">
<form>
<p id = "username"> Username </p>
<p id = "password"> Password </p>
<input type="text" name="user" id="textbox1">
<input type="password" name="pass" id="textbox2">
<input type = "button" value = "LOG IN" id = "press">
<p id = "note"> Note: Use the username and password provided by your department. …Run Code Online (Sandbox Code Playgroud) 以下是我的HTML页面:
<html>
<head>
<script type="text/javascript" src="/game/js/ready/main.js" ></script>
<link rel="stylesheet" type="text/css" href="/game/css/nav.css" />
</head>
<body>
<div class="nav-buttons">
<span class="nav-button" onclick="f()">Show A</span>
<span class="nav-button" onclick="h()">Show B</span>
<span class="nav-button" onclick="g()">Show C</span>
</div>
<div id="divA">
a
</div>
<div id="divB">
b
</div>
<div id="divC">
c
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
以下是我的main.js文件:
var divA = document.getElementById("divA");
var divB = document.getElementById("divB");
var divC = document.getElementById("divC");
function f(){
window.divA.style.display = "block";
window.divB.style.display = "none";
window.divC.style.display = "none";
}
function h(){
window.divA.style.display = "none";
window.divB.style.display = "block";
window.divC.style.display = "none";
} …Run Code Online (Sandbox Code Playgroud) 所以我正在使用这个有很多功能的 Javascript (API)。如何将其写入 HTML 文件?
所以其中一个函数是“api.ping()”,它在 powershell 上运行良好,但我无法将其打印在 HTML 文件中。
所以在我写的脚本中
document.getElementById("demo").innerHTML = api.ping();
和 HTML 是
<!DOCTYPE html>
<html>
<body>
<script type="index.js"></script>
<p id="demo"></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我试图将调用返回的值放到 HTML 文件中。
鉴于此html代码,我们不允许编辑
<!doctype html>
<html manifest="survey.manifest">
<head>
<title>Offline Survey Application</title>
<link rel="stylesheet" href="styles/survey.css" type="text/css">
<script type="text/javascript" src="scripts/survey.js"></script>
</head>
<body>
<!--should be empty --->
</body>
</html>
<!-- YOU ARE NOT ALLOWED TO EDIT THIS FILE!!! -->
Run Code Online (Sandbox Code Playgroud)
我似乎无法使我的JavaScript工作.但是,如果我将脚本元素放在主体上,则会显示整个脚本.问题是我们不允许这样做.有没有其他方法可以使它工作?
编辑:我知道该脚本在加载其他元素之前运行.我不能把它移到别处,因为文件会被修改:/
我想知道为什么会出现这个错误"未捕获的TypeError:无法读取null的属性'值'以及我哪里出错了?
function create()
{
var textbox=document.createElement("input");
var para=document.createElement("p");
para.setAttribute("id","p");
textbox.type='text';
textbox.value='asdf';
textbox.setAttribute("id","textbox");
textbox.setAttribute("onblur",document.getElementById('p').innerHTML=document.getElementById('textbox').value);
document.body.appendChild(textbox);
}
Run Code Online (Sandbox Code Playgroud)