2013-03-16 60 views
0

昨天我問了類似的問題,所以我知道如何將所有外部鏈接重定向到我的主頁。重定向除了我不想重定向的所有外部鏈接

// when the document is ready 
$(document).ready(function() { 
    // iterate over all the anchor tags 
    $("a").each(function() { 
     // if the current link's href doesn't already contain 'www.kownleg.com' 
     if (this.href.indexOf('www.kownleg.com') === -1) { 
      // change current link to home page 
      this.href = 'http://www.kownleg.com'; 
     } 
    }); 
}); 

但現在我要排除所有這些我不想重定向到我的主頁,像facebook.com & twitter.com的聯繫,我試圖讓該鏈接的另一個條件,我不要不想重定向,但它不起作用。然後我試圖改變indexOf()text()但仍然無法正常工作。


這裏是二值編碼,我試過,但我失敗了

// when the document is ready 
$(document).ready(function() { 
    // iterate over all the anchor tags 
    $("a").each(function() { 
     // if the current link's href doesn't already contain 'www.kownleg.com' 
     if (this.href.indexOf('www.kownleg.com') === -1) { 
      // change current link to home page 
      this.href = 'http://www.kownleg.com'; 
     } 
     if (this.href.indexOf('www.facebook.com') === -1) { 
      // change current link to home page 
      this.href = 'http://www.facebook.com'; 
     } 
     if (this.href.indexOf('www.twitter.com') === -1) { 
      // change current link to home page 
      this.href = 'http://www.twitter.com'; 
     } 
    }); 
}); 

另一個問題:

// when the document is ready 
$(document).ready(function() { 
    // iterate over all the anchor tags 
    $("a").each(function() { 
     // if the current link's href doesn't already contain 'www.kownleg.com' 
     if (this.href.indexOf('www.kownleg.com') === -1) { 
      // change current link to home page 
      this.href = 'http://www.kownleg.com'; 
     } 
     if (this.href.text('www.facebook.com') === -1) { 
      // change current link to home page 
      this.href = 'http://www.facebook.com'; 
     } 
     if (this.href.text('www.twitter.com') === -1) { 
      // change current link to home page 
      this.href = 'http://www.twitter.com'; 
     } 
    }); 
}); 

和所有的類似的可能性......但仍然沒有爲我工作。

回答

0

你需要用它四周的if語句:

if(
    (this.href.indexOf('www.twitter.com') !== -1) || 
    (this.href.indexOf('www.kownleg.com') !== -1) || 
    (this.href.indexOf('www.facebook.com') !== -1) 
){ 
    //do what you were doing. 
} 

希望這有助於。

+0

不工作.. :( – 2013-03-16 18:54:35

0

你可以試試這個:
在JavaScript

$(document).ready(function() { 
    var allowed_links_array = [ 
    "www.kownleg.com", 
    "www.twitter.com", 
    "www.facebook.com" 
    ]; 
    var replacement = [ 
    "http://www.kownleg.com", 
    "http://www.twitter.com", 
    "http://www.facebook.com" 
    ]; 
    // iterate over all the anchor tags 
    $("a").each(function() { 
    var index; 
    //get the index of the last part of the href attribute after the/
    index = jQuery.inArray(
     this.href.split('/')[this.href.split('/').length - 1], 
     allowed_links_array 
    ); 
    //if the link is in the allowed_links_array then set the href to the right link 
    if(index !== -1){ 
     this.href = replacement[index]; 
    } 
    }); 
}); 

working example您可以檢查錨標籤看到HREF改變。
我希望這是你正在尋找的。

+0

它不工作的傢伙,你沒有把鏈接,除了kownleg.com,Facebook和Twitter的,我把blogger.com你的腳本,你做的,但它不工作...檢查它 http://plnkr.co/edit/aAYuiXQadmIukpFIwoT0 – 2013-03-17 23:56:16

+0

它的工作,你需要總是添加允許的url到這兩個數組中的scripts.js文件在同一個索引,[檢查此](http:// plnkr。 CO /編輯/ 2NBAr2ZmgtvNKoMzvkyt?p =預覽)。 – 2013-03-18 04:22:03