2014-03-01 71 views
1

JavaScript我從HTML文件調用時無法正常工作。HTML不會調用帶有必需參數的javascript函數

下面是HTML文件代碼:

<li><a href="#" class="decorNavi" onclick ="xmlRequest('about')" >ABOUT</a></li> 

這裏是我的腳本:

function xmlRequest(target){ 
    var targetClick; 
    targetClick = target; 

    if (window.XMLHttpRequest) { 
     xmlRequest = new XMLHttpRequest(); 
    } 
    else{ 
     xmlRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlRequest.open("GET", "targetClick"+".html?="+Math.random() , true); 

    xmlRequest.onreadystatechange = function(){ 
    if (xmlRequest.readyState == 4 && xmlRequest.status ==200) { 
      document.getElementById("midContainer").innerHTML = xmlRequest.responseText; 
     } 
    } 

    xmlRequest.send(); 
} 

任何人都可以解釋我我的錯誤?請記住,我是初級網頁設計師,所以對於蹩腳的問題感到抱歉。

+1

xmlRequest(about)你期望的是什麼?一個字符串?比它必須是xmlRequest('約') –

+0

站在巨人的肩膀上。使用Javascript框架來保護你免受這些類型的錯誤等。jQuery是一個很好的例子,可以幫助你製作AJAX調用,以及更多...遊戲改變者。 –

+0

「約」在哪裏定義? – Bergi

回答

0

你混了變量,並將它們串represantations,繼承人修正版本

<li><a href="#" class="decorNavi" onclick ="xmlRequest('about')" >ABOUT</a></li> 

所以現在的youre傳遞一個字符串作爲參數

function xmlRequest(target){ 

     var targetClick; 

     //here you say the string argument now is called targetClick 
     targetClick = target; 

     if (window.XMLHttpRequest) { 

      xmlRequest = new XMLHttpRequest(); 
     } 

     else{ 
      xmlRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     //in this line you concatinate the variable with another string, result is a string so you dont need to quote the variable, this wouldnt work 
     xmlRequest.open("GET", targetClick+".html?="+Math.random() , true); 


     xmlRequest.onreadystatechange = function(){ 

      if (xmlRequest.readyState == 4 && xmlRequest.status ==200) { 

       document.getElementById("midContainer").innerHTML = xmlRequest.responseText; 
      } 
     } 

    xmlRequest.send(); 
} 

你需要在運行此網絡服務器,當使用「file://」協議時,你會得到一個交叉原點錯誤,另外該URL需要在同一主機上

+0

你是對的,我的網絡服務器出現故障,sublimeText開始在本地打開文件。非常感謝。 –

0

在你裏面,來自HTML的javascript調用,約必須被定義爲一個String。因此,與勁兒引號這樣括起來:

<li><a href="#" class="decorNavi" onclick ="xmlRequest('about')" >ABOUT</a></li> 

就像你來了,大約應該是一個JS變量,所以這是不確定的。

+0

這是語法錯誤的問題,在我的代碼中我使用單引號。 =) –

+0

檢查我的答案,在你的腳本中的另一個錯誤 –

0

我很確定你有一個錯誤,因爲未定義。它不應該是一個字符串?

<li><a href="#" class="decorNavi" onclick ="xmlRequest('about');" >ABOUT</a></li> 
0

targetClick現在是可變的,它不是字符串,所以你sholud做

xmlRequest.open("GET", targetClick+".html?="+Math.random() , true); 
//---should be without quotes--^ 

代替

xmlRequest.open("GET", 'targetClick'+".html?="+Math.random() , true); 
+0

是的,糾正錯誤仍然發生 –

0

不能裝載和XmlHttpRequest本地文件。將這些文件放入本地Web服務器。瀏覽器不會允許這樣做,因爲使用這種方式,遠程站點可以訪問您的本地文件,這是不允許的。

+0

我使用XAMPP模擬web服務器,其他腳本工作 –

+0

您的錯誤錯誤說您正在嘗試加載ile:/// C:/ xampp/htdocs/fgtest/experimentals/ZXC/about.html?= 0.3899174148682505。這是一個本地文件。瀏覽器只知道它在名爲'xampp'的文件夾中。它不會使用HTTP加載它,所以調用失敗。 –

+0

你是對的,我沒有手動做,但我使用的IDE確實打開它作爲本地文件,而不是作爲服務器上的文件,謝謝你的幫助 –

0

您是否定義了about-object?或者你想給過像這樣的字符串:與

<li><a href="#" class="decorNavi" onclick ="xmlRequest('about')" >ABOUT</a></li> 

注xmlRequest的(「關於」)調用「」,而不是xmlRequest(約)。