2013-02-27 93 views
0

我不知道爲什麼這個簡單的代碼,雖然它的意思是教育目的 所以請我幫忙找出我做了錯誤的事情:正在嘗試不能運行第二個警報

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Random thoughts part one</title> 
    <meta http-equiv="content-type" content="text/html;charset = utf-8" /> 
    <meta name="keywords" content="thoughts, random, philosophy, mohamed, samir ,sameer, muhammad , mohammed samir" /> 
    <meta name="description" content="This is a personal page about Mohamed Samir khalil that contain his thoughts and believes" /> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
     <body> 
     <script type="text/javascript">// 
      <![CDATA[ 

        alert("hello"); 
        var target = document.getElementById("unique_element"); 
        alert(target.length); 
      //]]> 
     </script> 
     <center> 
      <h3>Random thoughts</h3> 
     </center> 
     <p id="unique_element">I do not think it is coincidence, because every time I walk in streets I have that strong feeling that keeps telling me &#8221;Man, you have to help people!&#8221;.</p> 
     <p>In the beginning I thought that because of thinned for recognition I mean I do this just to hear people say &#8220;thank you&#8221; and to hear people say &#8220;it is great thing that you 
     are between us&#8221; so I thought it is all about ego, but you know when I really realized that it is not about ego!</p> 
     <p>I realized that when I helped someone and they did not say thank you, what was supposed to happen is that , I should feel anger because it is all about ego, right?</p> 
     <p>But what really happened that I felt happy just to help now people who are reading this will say either &#8220;you are so kind&#8221; or &#8220;Okay, you want to make it up huh!&#8221;but 
     the reality is just neither this or that ; actually I realized that I am helping people because this is what make us humans and this thing let us feel that we are not alone in this world so 
     it is kind of socializing too and also when you help people, I guarantee that someday someone will help you in a very tough situation and he or she will come out of nowhere this is the rule 
     of life and this is how people should live.</p> 
     </body> 
    </link> 
    </head> 
</html> 
+0

@RB。 Eurgh多麼可怕的想法 – George 2013-02-27 14:28:42

+2

我已經盡我所能地重新格式化了您的代碼,但Notepad ++在嘗試整理它時出錯了,所以我希望我沒有失去任何東西。請確保您在下次發佈問題時更好地格式化它。 – 2013-02-27 14:30:13

回答

3

腳本中的DOM解析之前,所以運行。嘗試在關閉</body> -tag之前放入JavaScript部分。

+0

或將其放入DOMContentLoaded監聽器 – 2013-02-27 14:36:36

+1

非常感謝,它運行良好 – Neo 2013-02-27 14:41:26

0

顯示DOM元素的長度。代替 嘗試alert(target.innerHTML.length);

var target = document.getElementById("unique_element"); 

不會返回HTML元素和target.length只會失敗: