【发布时间】:2016-06-18 01:47:08
【问题描述】:
当我点击链接并加载页面后单击 URL 时,我尝试更改 URL 链接(href 属性)。我试过这个:
<a href="http://example.com/ASC/" class="sort_loading_country">link</a>
jQuery( ".sort_loading_country" ).click(function() {
str = jQuery(".sort_loading_country").attr("href") ;
if ((str.indexOf('ASC') + 1) > 0) {
new_str = str.replace(/ASC/g, "DESC");
jQuery(".sort_loading_country").attr("href", new_str);
}
});
这会更改 URL,但在单击链接后会打开带有新 URL 的新页面。我想在打开页面并点击链接后更改 URL。我也试过这个:
jQuery(".sort_loading_country").click(function() {
str = jQuery(".sort_loading_country").attr("href");
jQuery(document).ready(function() {
if ((str.indexOf('ASC') + 1) > 0) {
new_str = str.replace(/ASC/g, "DESC");
jQuery(".sort_loading_country").attr("href", new_str);
}
});
同样的行为,点击链接 - 然后用新网址打开新页面。我希望当我单击带有旧网址的链接打开页面,然后在 href 中将网址更改为新的。 我错过了什么吗?有人知道如何在使用旧 URL 加载页面后更改属性 href 吗?
【问题讨论】:
-
方法#2 不行吗?
-
你的
a元素有 2 个href属性 -
您必须将
jQuery(document).ready()包裹在 所有 对 jQuery 的引用中。它应该是顶级包装器 -
抱歉,已删除href。是的,way2 不工作:(
-
任何人都知道如何在使用旧 URL 加载页面后更改属性 href 吗?
标签: javascript jquery html wordpress