2011-02-25 86 views
0

我有以下HTML:jQuery修改鏈接

<TD class=ms-sctabcf > 
    <A href="http://localdev.industrial.com:80/en-US/SearchM3/Pages/results.aspx?k=test+contenttype%3a%22IABusinessProduct%22&amp;s=IA_xx-XX_Content">All Results 
</A></TD> 

我需要一個jQuery函數刪除 「的contentType%3A%22IABusinessProduct%22」 從錨標記。

像這樣的東西應該這樣做:

function removeContentTypeFromAllResultsLinkOnGeneralSearch(){ 
    var anchorTag = $('.ms-sctabcf').has('text=All Results').find('a'); 
    var currentLink = anchorTag.link; 
    // remove the "contenttype%3a%22IABusinessProduct%22" from the link. (match any content type value using regular expression) 

    var modifiedLink = currentLink.regex.replace("contenttype%3a%22.*?%22",""); 
    anchorTag.link = modifiedLink; 
} 

你能幫我把我的僞代碼變成一個真正的jQuery/javascript函數?

此外,只有具有「所有結果」內部文本時,我才需要選擇一個標籤。什麼是jquery選擇器?

+2

這裏有什麼問題嗎? – justkt

+0

我認爲你的第二個代碼塊缺少一個報價。你找到了:) – Dubmun

回答

2

我想這是你想要什麼:

$("a").each(function() { 
    $(this).attr("href", $(this).attr("href").replace(/contenttype%3a%22IABusinessProduct%22/, "")); 
}); 
1
$("a").each(function(){ 
    var str = $(this).attr('href'); 
    var myhref = str.split('+contenttype'); 
    $(this).attr('href', myhref[0]); 
}); 

可能是更好的方法,但這種方式工作得很好。

0
(function($) { 
    function removeAllContentTypesFromLinks() { 
     var $a = $('a'); 
     $a.each(function() { 
      var $this = $(this), 
       href = $(this).attr('href').split('+contenttype')[0]; 
      $this.attr('href', href); 
     }); 
    } 
    removeAllContentTypesFromLinks(); 
}(jQuery));

這應該做的伎倆。一個jsfiddle在這裏:http://jsfiddle.net/LPwvp/4/