我需要使用jquery更新页面的href值。 请将href =“http://www.google.com?gsec=account”更改为href =“http://account.google.com?gsec=account”我如何完成此操作。我需要使用jquery更新href值
回答
这将做替换整个,我认为你正在寻找的页面。
// 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"]')
。
这不会修改没有“gsec = account”参数的google链接吗? – jmar777 2010-08-11 14:44:12
@ jmar777 - 是的。我不确定该特定参数是否是要求。将选择器更改为'$('a [href * =“gsec = account”]')'只会关注这些元素。 – user113716 2010-08-11 14:47:07
雅...这可能是98%的安全,但当然失败的yahoo.com?gsec =账户,或gsec = accountfoo等不知道如何_exact_他需要这件事。问题有些模糊。 – jmar777 2010-08-11 14:52:02
可以说你有这样的:
<a href="http://www.ibm.com" id="myLink">
你应该使用这样的:
var newHref = "http://google.com";
$("#myLink").attr('href', newHref);
我觉得你在你的问题遗漏了某些东西。尽管如此:
var link = $("a"); // Or some other selector to access the link
link.attr("href", "http://account.google.com?gsec=account");
使用jQuery的.attr()
方法。使用一个参数,它会返回属性值,并使用两个参数来设置它。
$("a#whatever").attr("href", "http://account.google.com?gsec=account");
这是一个重复:
它没有提到How to change the href for a hyperlink using jQuery
一种情况是,如果你设置需要进行更改,然后在你的锚一个id,你可以使用id作为jQuery中的选择器。
$("#LinkId").attr('href', "http://account.google.com?gsec=account")
如果要更改所有的链接,那么:
$(document).ready(function() {
$("a").attr("href", "http://account.google.com?gsec=account");
});
这样应该可以提供一个非常完整的解决方案,您的问题(如尽我所能解释它,反正):
$(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等查询字符串变量。
- 1. 使用jQuery更新href中的querystring值
- 2. 使用jQuery更改href值
- 3. 我需要更新我的jQuery代码
- 4. 需要更新与选择使用jQuery
- 5. 需要更新插件使用jQuery 1.4.3
- 6. jQuery/AJAX更新[href]?
- 7. 我需要使用jQuery验证与更新面板
- 8. jquery for li需要更新
- 9. 不能HREF更新使用jQuery
- 10. 我需要使用jQuery
- 11. 我需要使用jQuery
- 12. 我需要使用jQuery
- 13. 我需要使用jQuery
- 14. jquery - 基于选择值更新href?
- 15. 使用jquery更新页面中选定的href值
- 16. 用JQuery更新JQuery Mobile链接/ href
- 17. 需要用新值
- 18. 使用jQuery .eq更新值
- 19. 使用jquery更新值
- 20. 使用jQuery我需要用连字符
- 21. 使用JQuery刷新DIV不需要PHP
- 22. jquery href - 需要在mac和pc上使用
- 23. 需要使用JQuery切换每个href的div
- 24. pfbc需要更改使用jquery
- 25. 需要更换URL href和标签
- 26. 需要使用jQuery
- 27. 需要使用jQuery
- 28. 需要使用jQuery
- 29. 需要使用jQuery
- 30. 需要使用jQuery
我假设你需要一个动态的解决方案来搜索页面,并进行替换,而不是将一个预定的'href'分配给一个预定的''''元素。如果是这样,我在下面添加了一个答案,完成了这一点。 – user113716 2010-08-11 14:22:21
嗯......我发布了一个假设“帐户”是一个动态值,并且URL可能存在其他变体(例如http/https和其他查询字符串变量)。看到你的回应后,我可能会过度沮丧。 – jmar777 2010-08-11 14:27:43
@jmar - 是的,我的答案确实有一些关于什么被替换的要求。在选择器和'replace()'中使用正则表达式进行一些调整会使它更加开放。 – user113716 2010-08-11 14:35:17