2014-02-17 36 views
0

我需要從各個環節刪除鏈接從每個HREF

的頁面部分刪除鏈接的一部分,這樣

http://domain.com/download.php?url=https://www.dropbox.com/file1.rar 

我需要的鏈接是這樣

目前的連結
https://www.dropbox.com/file1.rar 

所以才刪除該http://domain.com/download.php?url=

這是我的代碼

<a href="http://domain.com/download.php?url=https://www.dropbox.com/file1.rar">Download File 1</a><br> 
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file2.rar">Download File 2</a><br> 
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file3.rar">Download File 3</a><br> 
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file4.rar">Download File 4</a><br> 
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file5.rar">Download File 5</a><br> 
<a href="http://domain.com/download.php?url=https://www.dropbox.com/file6.rar">Download File 6</a><br> 

<a href="https://www.dropbox.com/file9.rar">Download dropbox 1</a><br> 
<a href="https://www.dropbox.com/file8.rar">Download dropbox 2</a><br> 

<a href="https://www.google.com">Google</a><br> 
<a href="https://domain.com">HomePage</a><br> 

我設法選擇需要的jQuery更換

$的鏈接( 「一[HREF * = '的download.php?URL =']」)

但我需要幫助刪除此部分僅http://domain.com/download.php?url=

中的jsfiddle

的結果,我需要這樣的

http://jsfiddle.net/Jim_Toth/dB6nW/1/

回答

1

使用split()

$('a[href*="download.php?url="]').attr('href', function() { 
    return $(this).attr('href').split('=')[1]; 
}); 

注意隱含迭代,不需要each()

A live demo at jsFiddle

0

編輯:

$(function() { 
$("a[href*='download.php?url=']").each(function(i,v){ 
var oldUrl = $(this).attr('href'); 
var newUrl = oldUrl.replace("http://domain.com/download.php?url=",""); 
$(this).attr('href', newUrl); 
}); 
}); 
+0

'.replace()'希望第一個參數是正則表達式,不串(https://developer.mozilla.org/en -US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) – hindmost

+0

你可以試試'oldUrl.replace(/http.*url=/,'');' – algrebe

+1

@ behindmost [Nope ...]( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) – Teemu

1

這是解決您的問題:

$("a[href*='download.php?url=']").each(function(){ 
    var t = $(this); 
    var url = t.attr('href').replace('http://domain.com/download.php?url=', ''); 
    t.attr('href', url); 
}) 

http://jsfiddle.net/dB6nW/5/

0
var stringToRemove = 'http://domain.com/download.php?url='; 
$('a').each(function(){ 
    var link = $(this).attr('href'); 
    var newLink = link.replace(stringToRemove, ''); 
    console.log(newLink); 
}); 

有點簡化腳本。

0

我想你需要的是這樣的:

var links = $("a[href*='download.php?url=']"); 

for(var i = 0; i < links.length; i++){ 
var current = links.eq(i);  
     var href = current.attr("href"); 
    var newHref = href.substr(href.indexOf("="), href.length);  
    current.attr("href", newHref); 
} 

http://jsfiddle.net/dB6nW/7/