2014-01-07 158 views
-1

我需要一些幫助...我有這個網頁(http://93.62.201.235/maree/ESPORTAZIONI/MESE/Stazione_PuntaSalute_CanalGrande.html),它在威尼斯創建一個具有高潮值的表格。 我需要一個特定的值(在最後一個原始的第三個單元格),並得到它在一個變量...從外部HTML獲取單元格內容通過javascript

我試着用這個jquery代碼只看它是否與我在外部html頁面中創建的表:

function myFunction() 
{ 

$("#result").load("tableappend.html table", function(){ 
var y = document.getElementById("myTable2").rows[0].cells[0].innerHTML; 
console.log(y); 
}); 

} 

這是「外部」頁面的代碼:應該寫「小區1」上的console.log

<html> 

<body> 

<table id="myTable2" border="1"> 
    <tr> 
    <td>cell 1</td> 
    <td>cell 2</td> 
    </tr> 
    <tr> 
    <td>cell 3</td> 
    <td>cell 4</td> 
    </tr> 
</table> 
<br> 

</body> 
</html> 

,但它不工作... 任何解決方案?

+0

「它不起作用」是什麼意思?控制檯中有任何錯誤? –

+0

CORS怎麼樣? –

+0

沒有錯誤,它沒有顯示任何內容... – user3161604

回答

1

對於你需要考慮有關CORS(Cross Origin Resource Sharing)

你可以做到這一點使用JSONP JSON的任何跨域請求調用

或發送Origin頭爲您的請求的一部分。如果雙方當事人都同意之間共享,你可以把它成功

UPDATE:

由於您的問題沒有解釋什麼類型的錯誤,我們承擔並給予一定的答案

main.html中

<div id="result"></div> 

tableappend.html

<html> 

<body> 

<table id="myTable2" border="1"> 
    <tr> 
    <td>cell 1</td> 
    <td>cell 2</td> 
    </tr> 
    <tr> 
    <td>cell 3</td> 
    <td>cell 4</td> 
    </tr> 
</table> 
<br> 

</body> 
</html> 

在main.h tml

function myFunction() 
{ 
    //load the tableappend.html page, in the same site under same root 

    $("#result").load("tableappend.html table", 
      function(responseText, textStatus, XMLHttpRequest){ 
      alert(textStatus) // should be success 


    } 
} 
+0

但是如果它是一個跨域錯誤,OP應該在控制檯中出現錯誤。 –

+0

@ A.沃爾夫,OP甚至沒有解釋什麼錯誤:( –

+0

雅,真,無聊,我們要問......我的車壞了,該怎麼辦?該死... –