【问题标题】:Using .attr to change beginning of src property?使用 .attr 更改 src 属性的开头?
【发布时间】:2015-04-16 16:50:26
【问题描述】:

这改变了

  1. https://www.youtube.com/channel/blah”到
  2. https://www.youtube.com/channel/blah/videos
$(".yt-user-name.spf-link").attr("href", function(i, href) {  
    return href + '/videos';
});

我想知道如何将“https://www.youtube ...”更改为“https://www.m.youtube ...”

可能的解决方案(?):

  1. How to replace element's attr href with each ? // strip url
  2. https://stackoverflow.com/a/179717/2038928

【问题讨论】:

    标签: javascript jquery url attributes append


    【解决方案1】:

    $(document).ready(function(){
        $('a').each(function(){
            this.href = this.href.replace('https://www.youtube.com', 'https://www.m.youtube.com');
        });
    });

    【讨论】:

      【解决方案2】:

      你可以使用.replace来替换字符串的一部分:

      $(".yt-user-name.spf-link").attr("href", function(i, href) {
        return href.replace("www.you","www.m.you") 
      });
      

      【讨论】:

      • @computerguy 到底是什么问题?这是正确的答案。您只需将+ '/videos' 添加到末尾即可。
      • 我正在使用 tampermonkey 和 Firefox。这是我正在使用的代码:jsfiddle.net/computerguy/adrxkqp1
      猜你喜欢
      • 1970-01-01
      • 2012-10-09
      • 2016-05-12
      • 2013-06-01
      • 2023-03-12
      • 1970-01-01
      • 2013-02-24
      • 2022-08-14
      • 1970-01-01
      相关资源
      最近更新 更多