如何使用jQuery更改超链接的href属性(链接目标)?


当前回答

当单击类“menu_link”的链接时,此代码段将调用,并显示链接的文本和url。返回false将阻止链接被跟踪。

<a rel='1' class="menu_link" href="option1.html">Option 1</a>
<a rel='2' class="menu_link" href="option2.html">Option 2</a>

$('.menu_link').live('click', function() {
   var thelink = $(this);
   alert ( thelink.html() );
   alert ( thelink.attr('href') );
   alert ( thelink.attr('rel') );

   return false;
});

其他回答

当单击类“menu_link”的链接时,此代码段将调用,并显示链接的文本和url。返回false将阻止链接被跟踪。

<a rel='1' class="menu_link" href="option1.html">Option 1</a>
<a rel='2' class="menu_link" href="option2.html">Option 2</a>

$('.menu_link').live('click', function() {
   var thelink = $(this);
   alert ( thelink.html() );
   alert ( thelink.attr('href') );
   alert ( thelink.attr('rel') );

   return false;
});

根据您是希望将所有相同的链接更改为其他链接,还是希望仅控制页面给定部分中的链接,或者单独控制每个链接,您可以执行以下操作之一。

更改所有指向谷歌的链接,使其指向谷歌地图:

<a href="http://www.google.com">

$("a[href='http://www.google.com/']").attr('href', 
'http://maps.google.com/');

要更改给定节中的链接,请将容器div的类添加到选择器中。此示例将更改内容中的Google链接,但不会更改页脚中的链接:

<div class="content">
    <p>...link to <a href="http://www.google.com/">Google</a>
    in the content...</p>
</div>

<div class="footer">
    Links: <a href="http://www.google.com/">Google</a>
</div>

$(".content a[href='http://www.google.com/']").attr('href', 
'http://maps.google.com/');

要更改各个链接而不管它们在文档中的位置,请向链接添加一个id,然后将该id添加到选择器。此示例将更改内容中的第二个Google链接,但不会更改第一个或页脚中的链接:

<div class="content">
    <p>...link to <a href="http://www.google.com/">Google</a>
    in the content...</p>
    <p>...second link to <a href="http://www.google.com/" 
        id="changeme">Google</a>
    in the content...</p>
</div>

<div class="footer">
    Links: <a href="http://www.google.com/">Google</a>
</div>

$("a#changeme").attr('href', 
'http://maps.google.com/');

更改Wordpress Avada主题徽标图像的HREF

如果你安装了ShortCode Exec PHP插件,你可以创建这个我称为myjavascript的短代码

?><script type="text/javascript">
jQuery(document).ready(function() {
jQuery("div.fusion-logo a").attr("href","tel:303-985-9850");
});
</script>

现在,您可以转到外观/小部件,选择一个页脚小部件区域,并使用文本小部件添加以下短代码

[myjavascript]

选择器可能会根据您使用的图像以及视网膜是否就绪而改变,但您始终可以使用开发人员工具来确定。

Try

link.href = 'https://...'

link.href='https://stackoverflow.com'<a id=“link”href=“#”>单击我</a>

简单的方法是:

Attr函数(自jQuery 1.0版起)

$("a").attr("href", "https://stackoverflow.com/") 

or

Prop函数(自jQuery 1.6版起)

$("a").prop("href", "https://stackoverflow.com/")

此外,上述方法的优点是,若选择器选择单个锚点,它将仅更新该锚点,若选择器返回一组锚点,则它将仅通过一条语句更新特定组。

现在,有很多方法可以确定确切的锚点或锚点组:

非常简单的:

通过标记名称选择锚点:$(“a”)通过索引选择锚点:$(“a:eq(0)”)为特定类选择锚点(如在该类中仅锚点类活动):$(“a.active”)选择具有特定ID的锚点(在示例profileLink中ID):$(“a#proileLink”)选择第一个锚点href:$(“a:first”)

更有用的:

选择具有href属性的所有元素:$(“[href]”)选择具有特定href:$(“a[href='ww.stacksoverflow.com']”)的所有锚点选择不具有特定href:$(“a[href!='ww.stacksoverflow.com']”)的所有锚点选择具有包含特定URL的href的所有锚点:$(“a[href*='ww.stacksoverflow.com']”)选择以特定URL:$(“a[href^='ww.stacksoverflow.com']”)开头的所有锚点选择以特定URL结尾的所有锚点:$(“a[href$='ww.stacksoverflow.com']”)

现在,如果你想修改特定的URL,你可以这样做:

例如,如果您想为所有指向google.com的URL添加代理网站,可以按如下方式实现:

$("a[href^='http://www.google.com']")
   .each(function()
   { 
      this.href = this.href.replace(/http:\/\/www.google.com\//gi, function (x) {
        return "http://proxywebsite.com/?query="+encodeURIComponent(x);
    });
   });