2012-10-26 189 views
0

我想弄清楚如何編寫一個「switch語句」,所以如果一個特定的(我們稱之爲'yourdomain.com')外部URL匹配它不會在新窗口中打開,休息會。有人能幫助我教導我嗎?jQuery switch語句(?)

jQuery(function ($) { 
    $('a[href^="http://"]') 
     .not('[href*="http://mydomain.com"]') 
     .attr('target', '_blank'); 

例如,

mydomain.com =因爲這是我的網址

yourdomain.com這不會打開一個新窗口=這個特殊的URL不會在新窗口中打開,即使它是一個外部URL

anyotherdomain.com =除上述之外的所有其他外部URL將在新窗口中打開

+1

什麼似乎是問題解決了嗎?似乎對我很好。 http://jsfiddle.net/2PJPR/ –

回答

1

通過添加另一個。不能()

jQuery(function ($) { 
    $('a[href^="http://"]') 
     .not('[href*="http://mydomain.com"]').not('[href*="http://yourdomain.com"]') 
     .attr('target', '_blank'); 
1
$(function() { 
    var elms = $('a[href^="http://"]'); 
    $.each(elms, function() { 
      var current_link = $(this).attr("href"); 
      if (current_link == "yoururl") { 
       $(this).attr('target', '_blank'); 
      } 
    }); 
}); 

這個可以幫到您嗎?

+0

只是看着它,這將工作的一個特定的外部URL? (例如,mydomain.com是我的網址,我需要yourdomain.com不要在新窗口中打開,但其他任何網址都會打開。 –

+0

請查看:http://jsbin.com/ilixuq/1/edit是你的想要實現? –

+1

謝謝,安傑洛。我發佈了上面的解決方案,非常感謝您的幫助。 –