2012-03-13 27 views
0

我正在建立所有頁面使用Ajax加載的Wordpress網站。我使用Chris Coyer創建的腳本,可以找到here。下面的代碼將爲網站上的所有鏈接添加特定的標籤。 Wordpress將嘗試使用Ajax從這些鏈接加載內容。問題在於,這些屬性正在應用到PDF和ZIP文件的鏈接上,這些文件將被下載,而不是加載到頁面中。該腳本將特定的attr應用於網站上的所有鏈接。如何排除PDF和ZIP文件?

如何從獲取這些屬性中排除特定的文件格式?我已經嘗試過這樣的$internalLinks = $("a[href^='"+siteURL+"']:not([href$=/*.pdf])"),但那對我不起作用。難道我做錯了什麼?

下面是添加屬性的原始代碼的一部分。完整的代碼可以在JSFiddle page找到。謝謝!

var $mainContent = $("#main-content"), 
    siteURL = "http://" + top.location.host.toString(), 
    $el = $("a"); 

function hashizeLinks() { 
    $("a[href^='" + siteURL + "']").each(function() { 
     $el = $(this); 

     // Hack for IE, which seemed to apply the hash tag to the link weird 
     if ($.browser.msie) { 
      $el.attr("href", "#/" + this.pathname).attr("rel", "internal"); 
     } else { 
      $el.attr("href", "#" + this.pathname).attr("rel", "internal"); 
     } 
    }); 
};​ 

回答

1

不能使用*像一個文件規範通配符。只需使用這個:

$internalLinks = $("a[href^='"+siteURL+"'):not([href$='.pdf'])"); 
+0

我新我缺少的東西。這正是我正在尋找的。感謝gilly3! – Klikerko 2012-03-13 06:03:35

0

可以在所有匹配的鏈接應用濾鏡:

var $mainContent = $("#main-content"), 
siteURL = "http://" + top.location.host.toString(), 
$el = $("a"); 

function hashizeLinks() { 
$("a[href^='" + siteURL + "']").filter(function(){ 
     return this.href.match(/[^\.pdf]$/i); 
    }).each(function() { 
    $el = $(this); 

    // Hack for IE, which seemed to apply the hash tag to the link weird 
    if ($.browser.msie) { 
     $el.attr("href", "#/" + this.pathname).attr("rel", "internal"); 
    } else { 
     $el.attr("href", "#" + this.pathname).attr("rel", "internal"); 
    } 
}); 
};​ 
+0

感謝Nathan的快速回復。你的例子效果很好,但我會因爲簡單而使用@gilly3解決方案。 – Klikerko 2012-03-13 06:05:34

+0

這個傳說說簡單,它會繼續工作,我也會和他的解決方案一起去,我會注意到這一點。 :) – Nathan 2012-03-13 06:11:45

相關問題