2017-08-05 45 views
0

我在简单的代码中触发了,getelementbyid.innerHtml在html页面中。请帮帮我。 在html中使用getelementbyid获取空类型的错误

<!doctype html> 
 
<html lang="en"> 
 
\t <head> 
 
\t \t <title>Dom examples</title> 
 
\t \t <script> 
 
\t \t \t var code = document.getElementById("one").innerHTML; 
 
\t \t \t code = code + " !!"; 
 
\t \t \t console.log(code); 
 
\t \t </script> 
 
\t </head> 
 
\t <body> 
 
\t \t <h4> DOM practise page </h4> 
 
\t \t <p id="one"> This page is for practise of DOM model in JS</p> 
 
\t \t <p id="two"> Thank You </p> 
 
\t </body> 
 
</html>

+0

因为元素编号=一个尚未加载 –

+0

你试过'.innerText',而不是'.innerHTML'? – reporter

回答

0

您的JavaScript调用之前,您的浏览器渲染HTML页面。所以当你的浏览器调用getElementById时,ID没有被定义。

只需在文件末尾移动你的JavaScript

+0

在html标签之后移动script标签就足够了。 – reporter

0

只需移动下面的脚本...的造成是东阳的ID心不是渲染呢。

<!doctype html> 
 
<html lang="en"> 
 
\t <head> 
 
\t \t <title>Dom examples</title> 
 
\t \t 
 
\t </head> 
 
\t <body> 
 
\t \t <h4> DOM practise page </h4> 
 
\t \t <p id="one"> This page is for practise of DOM model in JS</p> 
 
\t \t <p id="two"> Thank You </p> 
 
\t </body> 
 
    <script> 
 
\t \t \t var code = document.getElementById("one").innerHTML; 
 
\t \t \t code = code + " !!"; 
 
\t \t \t console.log(code); 
 
\t \t </script> 
 
</html>

+0

我是JS和Html的新手初学者。你的建议帮助了我!非常感谢 !!有效 :-) –

0

利用的windowonload属性来调用一个函数时window加载

<!doctype html> 
 
<html lang="en"> 
 

 
<head> 
 
    <title>Dom examples</title> 
 
    <script> 
 
    onload = function() { 
 
     var code = document.getElementById("one").innerHTML; 
 
     code = code + " !!"; 
 
     console.log(code); 
 
    } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <h4> DOM practise page </h4> 
 
    <p id="one"> This page is for practise of DOM model in JS</p> 
 
    <p id="two"> Thank You </p> 
 
</body> 
 

 
</html>

0

只是把脚本部分近身之前,它会w ^扫。

<!doctype html> 
<html lang="en"> 
    <head> 
     <title>Dom examples</title> 
    </head> 
    <body> 
     <h4> DOM practise page </h4> 
     <p id="one"> This page is for practise of DOM model in JS</p> 
     <p id="two"> Thank You </p> 

     <script> 
      var code = document.getElementById("one").innerHTML; 
      code = code + " !!"; 
      console.log(code); 
     </script> 
    </body> 
</html> 
0

浏览器从上到下解析html页面。因此,如果您将script置于head中,它将首先呈现,然后是body。因此,您的script正在搜索尚未呈现给DOM的ID。

通过将script放置在底部,它可以解决您的问题。

<!doctype html> 
 
<html lang="en"> 
 
\t <head> 
 
\t \t <title>Dom examples</title> 
 
\t </head> 
 
\t <body> 
 
\t \t <h4> DOM practise page </h4> 
 
\t \t <p id="one"> This page is for practise of DOM model in JS</p> 
 
\t \t <p id="two"> Thank You </p> 
 
    <script> 
 
\t \t \t var code = document.getElementById("one"); 
 
\t \t \t code = code.innerHTML + " !!"; 
 
\t \t \t console.log(code); 
 
\t \t </script> 
 
\t </body> 
 
</html>

0

这是工作的罚款只是用script标签中结束。为了更清晰的检查。 https://jsfiddle.net/djmayank/Lzewxgyw/

HTML

<head> 
     <title>Dom examples</title> 

    </head> 
    <body> 
     <h4> DOM practise page </h4> 
     <p id="one"> This page is for practise of DOM model in JS</p> 
     <p id="two"> Thank You </p> 
    </body> 

JS

var code = document.getElementById("one").innerHTML; 
     code = code + " !!"; 
     console.log(code); 
0

script装载装有一个的ID的元素之前,因此一个尚不存在。您有以下选择:

  1. 你把标签后,您需要的脚本: <!doctype html> <html lang="en"> <head> <title>Dom examples</title> </head> <body> <h4> DOM practise page </h4> <p id="one"> This page is for practise of DOM model in JS</p> <p id="two"> Thank You </p> <script> var code = document.getElementById("one").innerHTML; code = code + " !!"; console.log(code); </script> </body> </html>
  2. 做一个function并使用onload属性,这是优选的,因为代码将是可重复使用的其他功能: <!doctype html> <html lang="en"> <head> <title>Dom examples</title> <script> function myFunction() { var code = document.getElementById("one").innerHTML; code = code + " !!"; console.log(code); } </script> </head> <body> <h4> DOM practise page </h4> <p id="one" onload="myFunction()"> This page is for practise of DOM model in JS</p> <p id="two"> Thank You </p> </body> </html>
相关问题