我需要使用jquery更新href值 [英] I need to update href value using jquery
问题描述
我需要使用jquery在页面上更新href值. 说href ="http://www.google.com?gsec=account"应该更改为href ="http://account.google.com?gsec=account"如何完成此操作.
I need to update href value thorughout the page using jquery. Say href="http://www.google.com?gsec=account" should be changed to href="http://account.google.com?gsec=account" how i can get this done.
推荐答案
这将替换整个我认为您要查找的页面.
This will do the replace throughout the page that I think you're looking for.
// 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.
的链接.
This version allows for https://
and for links without the 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"]')
.
If you only wanted to change elements that have gsec=account
, then change the selector to $('a[href*="gsec=account"]')
.
这篇关于我需要使用jquery更新href值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!