繁体   English   中英

如何使用 jQuery 更改超链接的 href 属性

[英]How to change the href attribute for a hyperlink using jQuery

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

使用

$("a").attr("href", "http://www.google.com/")

将修改所有超链接的 href 以指向 Google。 不过,您可能想要一个更精致的选择器。 例如,如果您混合使用链接源(超链接)和链接目标(又名“锚”)锚标记:

<a name="MyLinks"></a>
<a href="http://www.codeproject.com/">The CodeProject</a>

...那么您可能不想意外地向它们添加href属性。 为了安全起见,我们可以指定我们的选择器只匹配带有现有href属性的<a>标签:

$("a[href]") //...

当然,你可能会有更有趣的想法。 如果您想将锚点与特定的现有href匹配,您可以使用以下内容:

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

这将找到href与字符串http://www.google.com/完全匹配的链接。 一个更复杂的任务可能是匹配,然后只更新href一部分:

$("a[href^='http://stackoverflow.com']")
   .each(function()
   { 
      this.href = this.href.replace(/^http:\/\/beta\.stackoverflow\.com/, 
         "http://stackoverflow.com");
   });

第一部分仅选择 href 以http://stackoverflow.com开头的链接。 然后,定义一个函数,该函数使用简单的正则表达式将 URL 的这一部分替换为新的。 请注意这为您提供的灵活性 - 可以在此处对链接进行任何类型的修改。

使用 jQuery 1.6 及更高版本,您应该使用:

$("a").prop("href", "http://www.jakcms.com")

propattr的区别在于attr抓取 HTML 属性,而prop抓取 DOM 属性。

你可以在这篇文章中找到更多细节: .prop() vs .attr()

在查找中使用attr方法。 您可以使用新值切换出任何属性。

$("a.mylink").attr("href", "http://cupcream.com");

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

将所有指向 Google 的链接更改为指向 Google 地图:

<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/');

尽管 OP 明确要求提供 jQuery 答案,但如今您不需要对所有事情都使用 jQuery。

一些没有 jQuery 的方法:

  • 如果要更改所有<a>元素的href值,请将它们全部选中,然后遍历节点列表:(示例)

     var anchors = document.querySelectorAll('a'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });
  • 如果要更改所有实际具有href属性的<a>元素的href值,请通过添加[href]属性选择器( a[href] )来选择它们:(示例)

     var anchors = document.querySelectorAll('a[href]'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });
  • 如果要更改包含特定值(例如google.com<a>元素的href值,请使用属性选择器a[href*="google.com"] :(示例)

     var anchors = document.querySelectorAll('a[href*="google.com"]'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });

    同样,您也可以使用其他属性选择器 例如:

    • a[href$=".png"]可用于选择href值以.png结尾的<a>元素。

    • a[href^="https://"]可用于选择带有前缀https:// href值的<a>元素。

  • 如果要更改满足多个条件的<a>元素的href值:(示例)

     var anchors = document.querySelectorAll('a[href^="https://"], a[href$=".png"]'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });

..在大多数情况下不需要正则表达式。

这样做的简单方法是:

Attr 函数(自 jQuery 1.0 版起)

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

要么

Prop 函数(自 jQuery 1.6 版起)

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

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

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

很简单的:

  1. 通过标签名称选择锚点: $("a")
  2. 通过索引选择锚点: $("a:eq(0)")
  3. 选择特定类的锚点(因为在这个类中只有类active锚点): $("a.active")
  4. 选择具有特定 ID 的锚点(此处为profileLink ID 示例): $("a#proileLink")
  5. 选择第一个锚点 href: $("a:first")

比较有用的:

  1. 选择所有具有 href 属性的元素: $("[href]")
  2. 选择具有特定 href 的所有锚点: $("a[href='www.stackoverflow.com']")
  3. 选择所有没有特定 href 的锚点: $("a[href!='www.stackoverflow.com']")
  4. 选择所有带有包含特定 URL 的 href 的锚点: $("a[href*='www.stackoverflow.com']")
  5. 选择所有带有以特定 URL 开头的 href 的锚点: $("a[href^='www.stackoverflow.com']")
  6. 选择所有以特定 URL 结尾的 href 锚点: $("a[href$='www.stackoverflow.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);
    });
   });

当单击“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;
});

停止使用 jQuery 只是为了它! 仅使用 JavaScript 就如此简单。

document.querySelector('#the-link').setAttribute('href', 'http://google.com');

https://jsfiddle.net/bo77f8mg/1/

href在属性中,因此您可以使用纯 JavaScript 更改它,但是如果您已经在页面中注入了 jQuery,请不要担心,我会以两种方式展示它:

想象一下你在下面有这个href

<a id="ali" alt="Ali" href="http://dezfoolian.com.au">Alireza Dezfoolian</a>

你喜欢改变它的链接...

使用没有任何库的纯 JavaScript ,您可以执行以下操作:

document.getElementById("ali").setAttribute("href", "https://stackoverflow.com");

但也可以在jQuery中执行以下操作:

$("#ali").attr("href", "https://stackoverflow.com");

要么

$("#ali").prop("href", "https://stackoverflow.com");

在这种情况下,如果您已经注入了 jQuery,那么 jQuery 可能看起来更短且更跨浏览器......但除此之外,我选择了JS ......

 $("a[href^='http://stackoverflow.com']")
   .each(function()
   { 
      this.href = this.href.replace(/^http:\/\/beta\.stackoverflow\.com/, 
         "http://stackoverflow.com");
   });

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

如果你安装了 ShortCode Exec PHP 插件,你就可以创建这个我称之为 myjavascript 的 Shortcode

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

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

[myjavascript]

选择器可能会根据您使用的图像以及它是否已准备好视网膜而改变,但您始终可以使用开发人员工具来弄清楚。

尝试

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

 link.href = 'https://stackoverflow.com'
 <a id="link" href="#">Click me</a>

<a class="link">My Link</a>
<script>
$(document).ready(function(){
    $("a.link").attr("href", "https://toptruyenfull.com/")
})
</script>

尝试这个;

$("#link").attr("href", "https://coenvink.com/")

代码功能的细分:

$("#link")

这部分代码获取 ID 为“Link”的元素。 在此之后,您将属性 'href'(女巫基本上是指向 url 的链接)设置为您的新网址,在本例中,女巫是我自己的网站:

.attr("href", "https://coenvink.com/")

我希望现在清楚了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM