2013-05-14 104 views
0

即時通訊工作與jquery加載功能,需要填寫div的內容從頁面到另一頁。jquery加載函數不工作在鉻在Firefox中工作?

first.html

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script> 
$('#getselect').find('option:selected').text(); 
</script> 
</head> 
<body> 
<div id="getdiv">Not loaded yet.</div> 
</body> 
</html> 

second.html

<!DOCTYPE html> 
    <html> 
    <head> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    </head> 
    <body> 
    <b>Footer navigation:</b> 
    <div id="getit"></div> 
    <div id="getitselected"></div> 
    <script> 
    $("#getit").load("first.html #getdiv"); 
    </script> 
    </body> 
</html> 

該腳本能正常工作在Firefox,但在CHROM不工作。請幫幫我。

+0

不工作意味着 – PSR 2013-05-14 09:23:23

+0

刪除first.html中的所有html,head,body標籤。 – 2013-05-14 09:23:41

+0

你是否假設第一個文件中的jQuery代碼將在第二個文件中設置選擇? – adeneo 2013-05-14 09:26:05

回答

1

嘗試在$(document).ready()

$(document).ready(function(){ 
    $("#getit").load("first.html #getdiv"); 
}); 
+6

爲什麼會有幫助? – adeneo 2013-05-14 09:26:26

+0

@kevin bowersox我試過了,但沒有工作! – 2013-05-14 13:11:40

+0

如果代碼不在主體的底部(並且因此在DOM準備就緒時已經執行),這隻會有所作爲。 – 2014-06-06 11:22:49

0

這是一個AJAX請求,如果你嘗試直接從second.html first.html使用Chrome瀏覽器將不會運行它包裝一下你的代碼,因爲same-origin-policy

認沽它在server(本地/非本地),並嘗試運行它像localhost:3000/testProject/second.html,它會正常工作。

0

或者你可以打開瀏覽器這樣

cd C:\Program Files\Google\Chrome\Application 
start chrome.exe C:\second.html --disable-web-security 
1

如果不將網站託管在服務器上,你只嘗試從瀏覽器中打開一個.html文件,在這種情況下,Chrome瀏覽器無法訪問另一個文件在你的文件系統中。 您必須將您的項目部署到服務器,例如打印並測試它。你會看到鉻工作正常。

0

使用preventdefault()

$(document).ready(function(){ 
    event.preventDefault(); 
    $("#getit").load("first.html #getdiv"); 
}); 
+0

很高興解釋爲什麼這會有所作爲。 – 2014-03-05 12:29:54

+0

-1:誰贊成這個?這完全是無稽之談。 'preventDefault()'不適用於DOM就緒事件(即使它沒有做任何事情,'event'在你的代碼範圍內也不存在*)。 :( – 2014-06-06 11:25:54

+0

是的,你是對的,但它的工作是爲這種情況完成的,對於有人喜歡這個有用, – 2014-06-06 16:01:38

0

https://stackoverflow.com/a/2990622/1123295http://www.chrome-allow-file-access-from-file.com/

您將需要與--allow-文件訪問從-文件運行Chrome,以jQuery的負載功能,在本地工作。

如果在Windows 7+中,將Chrome固定到任務欄並右鍵單擊該圖標,然後右鍵單擊「Google Chrome」並選擇「屬性」。然後更改目標以獲得新標誌,如下所示:

「C:\ Program Files(x86)\ Google \ Chrome \ Application \ chrome.exe」 - 允許從文件訪問文件

現在每次chrome啓動時,它都會使用新策略。

+0

我和OP有同樣的問題,修理它 – mgrenier 2015-04-02 19:22:05

相關問題