2011-06-14 23 views
1

您好我有此腳本:如何檢測我的JavaScript中的鏈接類?

function ajax(){ 
    if (navigator.standalone) return; 
    for (var i= document.links.length; i-->0;) { 
     document.links[i].onclick= function() { 
      var req= new XMLHttpRequest(); 
      req.onreadystatechange= function() { 
       if (this.readyState!==4) return; 
       document.body.innerHTML= this.responseText; 
       ajax(); 
      }; 
      req.open('GET', this.href, true); 
      req.send(); 
      return false; 
     };} 
    } 



window.onload= function() { 
    window.scrollTo(0, 0.9); 
    ajax(); 

}; 

現在我只想補充一點1個東西。如果一個鏈接有一個名爲「轉產」類中的AJAX不應執行(annother頁面應該加載).I've嘗試的東西,但我只是沒有用JavaScript不夠好,得到這個權利:

function ajax(){ 
    if (navigator.standalone) return; 
    for (var i= document.links.length; i-->0;) { 
     if (document.links[i].getAttribute("class") == "noeffect") return; 
     document.links[i].onclick= function() { 
      var req= new XMLHttpRequest(); 
      req.onreadystatechange= function() { 
       if (this.readyState!==4) return; 
       document.body.innerHTML= this.responseText; 
       ajax(); 
      }; 
      req.open('GET', this.href, true); 
      req.send(); 
      return false; 
     };} 
    } 



window.onload= function() { 
    window.scrollTo(0, 0.9); 
    ajax(); 

}; 

我猜它會檢查所有鏈接,而不是每個鏈接,但我不知道如何以正確的方式對其進行編碼。 :s

+0

預期的行爲是什麼?出了什麼問題? – Dan 2011-06-14 22:54:21

+0

什麼都沒有發生。但我已經解決了,無論如何謝謝你:) – cmplieger 2011-06-14 22:55:20

回答

0

想通了!

function ajax(){ 
    if (navigator.standalone) return; 
    for (var i= document.links.length; i-->0;) { 
     document.links[i].onclick= function() { 
      if(this.getAttribute("class") == "noeffect") return; 
      var req= new XMLHttpRequest(); 
      req.onreadystatechange= function() { 
       if (this.readyState!==4) return; 
       document.body.innerHTML= this.responseText; 
       ajax(); 
      }; 
      req.open('GET', this.href, true); 
      req.send(); 
      return false; 
     };} 
    } 



window.onload= function() { 
    window.scrollTo(0, 0.9); 
    ajax(); 

};