使用.attr更改src属性的开头?

时间:2015-02-16 08:07:50

标签: javascript jquery url attributes append

此更改

  1. https://www.youtube.com/channel/blah”至
  2. https://www.youtube.com/channel/blah/videos
  3. $(".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

2 个答案:

答案 0 :(得分:3)

您可以使用.replace替换字符串部分:

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

答案 1 :(得分:0)

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