简体   繁体   English

document.getElementById不起作用

[英]document.getElementById doesn’t work

If I write code 2 without code 1, the code works and it shows me “aaaaa”. 如果我写的代码2 不带代码1,则该代码有效,并且显示“ aaaaa”。

But if I write code 1 and code 2, the code doesn't work. 但是,如果我编写代码1 代码2,该代码将无法正常工作。 Instead of showing me “vvvaa”, it doesn't show me anything (not “aaaaa” and not “vvvaa”). 它没有向我显示“ vvvaa”,没有显示给我任何东西(不是“ aaaaa”,也不是“ vvvaa”)。

Why doesn't it work? 为什么不起作用? (The document.getElementById doesn't send the information to the <div> .) document.getElementById不会将信息发送到<div> 。)

Code 1: 代码1:

document.getElementById('na').innerHTML = "vvvaa";

Code 2: 代码2:

document.write("<div id='na'> aaaaa </div>");

Complete Code: (the only thing on the page) 完整代码:( 页面上唯一的内容)

<script>
    function timeago(time) {
        var new_date = new Date();
        var time_ago = Math.floor(new_date.getTime()/1000-time);
        var d = Math.floor(time_ago/24/60/60);
        var h = Math.floor((time_ago-d*24/60/60)/60/60);        
        var m = Math.floor((time_ago-d*24/60/60-h*60/60)/60);
        var s = Math.floor(time_ago-d*24/60/60-h*60/60-m*60);
        document.write(d+"d - "+h+"h - "+m+"m - "+s+"s");
        document.getElementById('na').innerHTML="vvvaa";
        // setTimeout( function(){ timeago(time); }, 2000 ); 
    }
    timeago('1376743609');
    document.write("<div id='na'> aaaaa </div>");
</script>

Order matters. 顺序很重要。 You cannot access your element 'na' before having it in the document. 在文档中添加元素'na'之前,您无法对其进行访问。

You naturally need to add the element to the document first. 您自然需要首先将元素添加到文档中。 If that's done, you can access it by functions like getElementById() . 完成后,您可以通过类似getElementById()函数来访问它。

This... 这个...

document.write("<div id='na'></div>");
document.getElementById('na').innerHTML = "vvvaa";

... will work. ... 将工作。

You may shortcut this to: 您可以将此快捷方式更改为:

document.write("<div id='na'>vvvaa</div>");

I am assuming your console says that document.getElementById('na') is undefined and innerHTML is not a method of undefined. 我假设您的控制台说document.getElementById('na')是未定义的,而innerHTML不是未定义的方法。 This is caused by the fact that there is no such element when the code is called. 这是由于在调用代码时没有这样的元素引起的。 A fatal error will stop any further javascript execution, in this case your document.write . 致命错误将停止任何进一步的javascript执行,在这种情况下,您将执行document.write

Add the element to your document first before trying to access it via document.getElementById 在尝试通过document.getElementById访问元素之前,请先将其添加到document.getElementById

You can't access a piece of text unless it really does exist. 除非文本确实存在,否则您无法访问它。 In your case, you are trying to access the text when it doesn't even exist at that point. 对于您的情况,您正在尝试访问当时还不存在的文本。 The order matters. 顺序很重要。 Code 2 should go first and Code 1 should go last. 代码2应该排在最前面,代码1应该排在最后。 First write the text, then access it. 首先写文本,然后访问它。

The document.write only be passed after timeago() therefore does not exist over the <div> , so just call "timerago" after using document.write Try: document.write仅在timeago()之后传递,因此在<div>上不存在,因此在使用document.write之后只需调用“ timerago”即可:

<script>
function timeago(time) {
    var new_date = new Date();
    var time_ago = Math.floor(new_date.getTime()/1000-time);
    var d = Math.floor(time_ago/24/60/60);
    var h = Math.floor((time_ago-d*24/60/60)/60/60);        
    var m = Math.floor((time_ago-d*24/60/60-h*60/60)/60);
    var s = Math.floor(time_ago-d*24/60/60-h*60/60-m*60);
    document.write(d+"d - "+h+"h - "+m+"m - "+s+"s");
    document.getElementById('na').innerHTML="vvvaa";
    // setTimeout( function(){ timeago(time); }, 2000 ); 
}
document.write("<div id='na'> aaaaa </div>");
timeago('1376743609');
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM