【问题标题】:Can the domain names be changed in hyperlinks?可以在超链接中更改域名吗?
【发布时间】:2017-07-18 01:00:57
【问题描述】:

就像<code><div></code> 中的动态链接一样,我可以更改所有超链接的域名吗?

例如:-

这是超链接链接标签

<a href="http://onedomain.com/offers/testing-something/">

我可以把这个改成

<a href="http://seconddomain.com/offers/testing-something/">

动态地使用 jquery。链接中的所有其他参数应相同,但应更改域名。如果可以的话,请解释一下,我将不胜感激。

【问题讨论】:

标签: javascript jquery html hyperlink


【解决方案1】:

只要做:

$('a').each(function(){
  $(this).attr('href').replace("OLD-DOMAIN","NEW-DOMAIN");
});

【讨论】:

  • 域名后的参数修改后保持不变
  • 我没有投反对票,但我首先得到了较高的答案,这就是为什么我首先投赞成票
【解决方案2】:

使用replace():-

$(document).ready(function(){
   $('a').each(function(){
        this.href = this.href.replace('onedomain.com', 'seconddomain.com');
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://onedomain.com/offers/testing-something/">link</a>

@satpal 解决方案也可以使用:-

$(document).ready(function() {
  $('a').attr('href', function(_, value) {
    return value.replace('onedomain.com', 'seconddomain.com');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://onedomain.com/offers/testing-something/">link</a>

【讨论】:

  • 你也可以推荐$('a').attr('href', function(_, value){ return value.replace('onedomain.com', 'seconddomain.com'); });
【解决方案3】:

您可以使用prop( propertyName, function ) 简单地更改&lt;a&gt; 元素的host 属性

$('a').prop('host',function(){
  return 'seconddomain.com';
})

// for demo loop over elements and log adjusted href
.each(function(){
  console.log(this.href)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://onedomain.com/offers/testing-something/">
<a href="http://onedomain.com/offers/something-else/">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-18
    • 2021-10-30
    • 2014-01-11
    • 1970-01-01
    • 2017-07-01
    • 2013-02-04
    • 2015-10-11
    • 1970-01-01
    相关资源
    最近更新 更多