2010-08-11 43 views
2

我需要使用jquery更新页面的href值。 请将href =“http://www.google.com?gsec=account”更改为href =“http://account.google.com?gsec=account”我如何完成此操作。我需要使用jquery更新href值

+0

我假设你需要一个动态的解决方案来搜索页面,并进行替换,而不是将一个预定的'href'分配给一个预定的''''元素。如果是这样,我在下面添加了一个答案,完成了这一点。 – user113716 2010-08-11 14:22:21

+0

嗯......我发布了一个假设“帐户”是一个动态值,并且URL可能存在其他变体(例如http/https和其他查询字符串变量)。看到你的回应后,我可能会过度沮丧。 – jmar777 2010-08-11 14:27:43

+0

@jmar - 是的,我的答案确实有一些关于什么被替换的要求。在选择器和'replace()'中使用正则表达式进行一些调整会使它更加开放。 – user113716 2010-08-11 14:35:17

回答

7

这将做替换整个,我认为你正在寻找的页面。

// Find `<a>` elements that contain `www.google.com` in the `href`. 
$('a[href*="www.google.com"]').attr('href', function(i,href) { 
     // return a version of the href that replaces "www." with "accounts." 
    return href.replace('www.', 'accounts.'); 
}); 

试试看:http://jsfiddle.net/dT8j6/


编辑:该版本允许https://并没有www.链接。

试试看:http://jsfiddle.net/dT8j6/1/

$('a[href*="google.com"]').attr('href', function(i,href) { 
    return href.replace(/^http(s*):\/\/(www\.)*google.com/, 'http$1://accounts.google.com'); 
}); 

编辑:如果你只是想改变这种有gsec=account元素,然后选择更改为$('a[href*="gsec=account"]')

+0

这不会修改没有“gsec = account”参数的google链接吗? – jmar777 2010-08-11 14:44:12

+0

@ jmar777 - 是的。我不确定该特定参数是否是要求。将选择器更改为'$('a [href * =“gsec = account”]')'只会关注这些元素。 – user113716 2010-08-11 14:47:07

+0

雅...这可能是98%的安全,但当然失败的yahoo.com?gsec =账户,或gsec = accountfoo等不知道如何_exact_他需要这件事。问题有些模糊。 – jmar777 2010-08-11 14:52:02

3

可以说你有这样的:

<a href="http://www.ibm.com" id="myLink"> 

你应该使用这样的:

var newHref = "http://google.com"; 

$("#myLink").attr('href', newHref); 
0

我觉得你在你的问题遗漏了某些东西。尽管如此:

var link = $("a"); // Or some other selector to access the link 
link.attr("href", "http://account.google.com?gsec=account"); 
0

使用jQuery的.attr()方法。使用一个参数,它会返回属性值,并使用两个参数来设置它。

$("a#whatever").attr("href", "http://account.google.com?gsec=account"); 
1

这是一个重复:

它没有提到How to change the href for a hyperlink using jQuery

一种情况是,如果你设置需要进行更改,然后在你的锚一个id,你可以使用id作为jQuery中的选择器。

$("#LinkId").attr('href', "http://account.google.com?gsec=account") 
0

如果要更改所有的链接,那么:

$(document).ready(function() { 
$("a").attr("href", "http://account.google.com?gsec=account"); 
}); 
1

这样应该可以提供一个非常完整的解决方案,您的问题(如尽我所能解释它,反正):

$(function() { 
    $('a').each(function() { 
    var $this = $(this), 
     href = $this.attr('href'); 
    var res = href.match(/(.*?)(www)(\.google\.com.*?)([?&]gsec=)([^&]+)(.*)/); 
    if (null != res) { 
     // remove the "full match" entry 
     res = res.slice(1); 
     // replace www match with account match 
     res[1] = res[4]; 
     // update the href attribute 
     $this.attr('href', res.join('')) 
    } 
    }); 
}); 


编辑:如果“账户”是一个静态值,那么这个也能发挥作用:

$(function() { 
    $('a').each(function() { 
    var $this = $(this), 
     href = $this.attr('href'); 
    var res = href.match(/(.*?\/\/)(www)(\.google\.com.*?)([?&]gsec=account)(&?.*)/); 
    if (null != res) { 
     // remove the "full match" entry 
     res = res.slice(1); 
     // replace www match with account match 
     res[1] = 'account'; 
     // update the href attribute 
     $this.attr('href', res.join('')) 
    } 
    }); 
}); 

请注意,这些解决方案假定有可能在URL其他变化,如HTTP/HTTPS等查询字符串变量。