2013-03-16 63 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