2013-03-09 67 views
1

我一直在試圖得到這個照顧,並且它似乎並不想工作。我搜索了互聯網和stackoverflow的答案,但我似乎無法找到任何實際的工作。如果任何人都可以幫助我,那太棒了!爲什麼這個JQuery內部html集合不起作用?

<html> 
    <head> 
     <script rel="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> 

     $(document).ready(function(){ 
      $(".box").html("sing!!!!"); 
     }); 
     </script> 

     <style> 
      .box{ 
      border:2px solid black; 
      padding:12px; 
      } 
     </style> 
    </head> 

    <body> 
     <div class="box"> 
      This will be replaced with a JQuery statement. 
     </div> 
     <p> 
      This is text to be left UNALTERED. 
     </p> 
    </body> 
</html> 
+4

這可能是因爲你正在加載jQuery庫和聲明腳本語句在一個標籤。請參閱http://stackoverflow.com/questions/6528325/what-does-a-script-tag-with-src-and-content-mean – 2013-03-09 18:02:44

+0

附註:'script'標籤沒有'rel'屬性,而' type'屬性默認爲'text/javascript',所以你應該把'rel'完全關閉,如果你喜歡(如果你使用的是JavaScript),你可以*關閉'type'。 – 2013-03-09 18:10:42

回答

4

jquery腳本標記未關閉。

<script rel="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 

<script> 
     $(document).ready(function(){ 
      $(".box").html("sing!!!!"); 
     }); 
</script> 
+0

好的,這是一個單獨的腳本標籤集頭內?好吧,我想這是有道理的。謝謝! – Articulous 2013-03-09 22:32:20

3

script tag可以要麼具有src屬性加載外部文件,或內嵌內容,但從來沒有兩者。如果同時提供,內聯內容將被忽略(在大多數瀏覽器上)。

所以你script標籤:

<script rel="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> 

$(document).ready(function(){ 
    $(".box").html("sing!!!!"); 
}); 
</script> 

...是無效的。您需要結束一個裝載jQuery的,然後打開一個新一個爲您的代碼:

<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $(".box").html("sing!!!!"); 
}); 
</script> 

(見我的爲什麼我已經刪除rel並從type問題評論)