2015-01-21 73 views
0

我想在一個頁面中獲得新聞網站的最新新聞部分。 我已經找到了關於jQuery中的.load函數的能力。但是如何?.load函數調用特定的類

<html> 
    <head> 
    <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
    <script>$("#today").load(http://www.today.mn + '.lastnews-wrapper'), function(data){};</script> 
    </head> 
    <body> 
    <div id="today" style="width:400px; height:700px border:1px solid; border:#FF2;"></div> 
    </body> 
</html> 

到目前爲止,這是我做的。但屏幕仍然是空的

+0

.load()從服務器加載東西而不是互聯網。我認爲你所追求的是Ajax,如果有新的數據可用,則重新加載。 – SharpCode 2015-01-21 00:39:56

+0

語法錯誤:您必須將url包含在引號中,並刪除',function(data){}'部分......概念性錯誤:您最終可能無法加載內容,因爲[CORS](http:/ /en.wikipedia.org/wiki/Cross-origin_resource_sharing) – 2015-01-21 00:44:52

回答

0

語法錯誤(load()語法,css,url,加載回調等)和可能的原點限制。

試試這個 - 假設容器:.lastnews-wrapper實際上保存了您之後的數據。

<html> 
    <head> 
     <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
     <script> 
     $(function(){ 
      $("#today").load("http://www.today.mn .lastnews-wrapper",function(responseTxt,statusTxt,xhr){ 
       if(statusTxt=="success") 
        console.log("Loaded successfully!"); 
       if(statusTxt=="error") 
        console.log("Error: "+xhr.status+": "+xhr.statusText); 
      }); 
     }); 
     </script> 
    </head> 
<body> 
    <div id="today" style="width:400px; height:700px; border:1px solid #FF2;"></div> 
</body> 
</html> 

看看控制檯,你應該看到失敗(或成功)消息的原因。

享受。