【问题标题】:Adding a href to existing one向现有的添加一个 href
【发布时间】:2017-11-01 10:53:51
【问题描述】:

我正在抓取一个新闻网站,他们还提供了整篇文章的链接,但是这些链接的 href 如下所示:

/news-features/8/news-headlines/103818/these-pupils-deserve-better

所以为了链接我需要动态添加:

http://www.oldham-chronicle.co.uk

所以整个链接是:

http://www.oldham-chronicle.co.uk/news-features/8/news-headlines/103818/these-pupils-deserve-better

您可以假设有超过 1 篇文章,但是我需要添加的链接部分是相同的。因此,我需要为它们中的每一个添加它。

目前我有:

$("a").each(function(){
    this.href=this.href.replace("http://www.oldham-chronicle.co.uk");
});

但是我的链接看起来像这样:

href="http://localhost/news-features/8/news-headlines/103818/these-pupils-deserve-better"

哪里错了,怎么解决?

【问题讨论】:

  • 这并没有错,真的..您的链接呈现为绝对链接,在这种情况下 localhost 是您的好..您网站的本地 Web 服务器。你的函数实际上并没有替换任何东西..你想要做的是添加第二个参数 - 目前你只是用空字符串替换 oldham-chroncile..
  • 可以举个例子吗?工作8小时后我的大脑有点慢哈哈
  • 我猜奥德姆编年史已经允许你对它们进行筛选...?!
  • 是的,我们是一家企业,我们被允许这样做;)不用担心,在开始做之前它已经全部排序

标签: javascript jquery


【解决方案1】:

试试这个:

var base = "http://www.oldham-chronicle.co.uk/";
$('a').each(function(index, element) {
  element.href = element.href.replace("http://localhost/", base);
})

基本上,这会循环遍历每个元素并预先添加您想要的硬编码 URL。 (如果需要,您也可以在没有 jquery 的情况下执行此操作)

编辑:误解了原始问题,从评论中更新以替换开头的url(使用简单的匹配器)

【讨论】:

【解决方案2】:

这对你有用

$("a").each(function(){
     if ($(this).attr('href') != null) {
        var newUrl = $(this).attr('href').replace("http://localhost","http://www.oldham-chronicle.co.uk");
        if (newUrl.indexOf("http:")==-1) {newUrl  = "http://www.oldham-chronicle.co.uk"+newUrl; }
        $(this).attr('href', 
         );
     }
});

【讨论】:

  • 不,它没有链接仍然是相同的:/news-features/8/news-headlines/103818/these-pupils-deserve-better
  • 我修好了,现在正在检查url是否没有域名
【解决方案3】:

对于使用现代浏览器的读者,您可以使用the hostname property

$('a').each((i, e) => e.hostname = 'www.oldham-chronicle.co.uk');

请注意,这在 Internet Explorer 和其他一些浏览器中不起作用,如链接页面所示。此示例还使用了箭头功能,某些旧版浏览器不支持这些功能。

【讨论】:

    【解决方案4】:

    您可以通过执行以下操作来避免使用.each

    $('a.link').attr('href', function(i, v){
      console.log("replaced href:");
      console.log(v);
      return v.replace('http://localhost', function() {
          return 'www.oldham-chronicle.co.uk';
      })
    });
    
    // just to show new href
    $('a.link').attr('href', function(i, v) {
        console.log("new href:");
    	console.log(v);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a class="link" href="http://localhost/news-features/8/news-headlines/103818/these-pupils-deserve-better">Link1</a>
    <a class="link" href="http://localhost/news-features/7/news-headlines/103818/these-pupils-deserve-better">Link2</a>

    感谢Denys answer here

    【讨论】:

      【解决方案5】:

      试试这个:

      $("a").each(function(){
       $(this).attr("href", "http://localhost/news-features/8/news-headlines/103818/these-pupils-deserve-better");
      });
      

      【讨论】:

      • 不,这会将所有链接设置为该链接-他想将 localhost 替换为域-而不仅仅是获取该页面的每个链接
      • 这是错误的,因为我将为所有文章设置相同的链接,并且每篇文章都有不同的链接
      • 1) 有一个错字(双双引号)和 2) 你完全错过了 URL 的第二部分
      猜你喜欢
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 2018-04-15
      • 2021-11-04
      • 2014-11-04
      相关资源
      最近更新 更多