2013-08-01 77 views
0
<!DOCTYPE html> 
<html> 
    <head> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
     <script> 
      $(document).ready(function() 
      { 
       $("button").click(function() 
       { 
        $("#div1").load("http://w3schools.com/jquery/demo_test.txt"); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <div id="div1"> 
      <h2>Let jQuery AJAX Change This Text</h2> 
     </div> 
     <button>Get External Content</button> 
    </body> 
</html> 

上述代碼是W3Schools中的一個示例,我嘗試在瀏覽器中運行它。雖然它適用於W3schools窗口,但當我嘗試從我的html頁面運行它時,它拒絕。Jquery .load函數未按預期工作

+0

*「PS請只有建設性的答案」*這是Stackoverflow,只有建設性的答案張貼... –

+0

請不要使用W3Schools。永遠。 http://w3fools.com –

+0

[學習調試javascript](http://www.netmagazine.com/tutorials/javascript-debugging-beginners) –

回答

3

不!您無法使用JavaScript從遠程網站加載頁面。這違反了Same Domain Policy

+0

有沒有其他辦法可以實現這一點? –

+0

通過醜陋的手段,是的。例如,div元素可以包含一個填充div空間100%的iframe,iframe指向一個遠程頁面。 雖然您無法輕鬆與iframe的內容進行互動,但... – Brian

+0

http://coding.smashingmagazine.com/2012/02/09/beginners-guide-jquery-based-json-api-clients/ –