Javascript Jquery没有选择正确的href';s

Javascript Jquery没有选择正确的href';s,javascript,jquery,url,href,Javascript,Jquery,Url,Href,此脚本: $(function() { $('a').each(function() { var href = $(this).attr('href'); if ("a:not(http://)") { $(this).attr('href', '/' + href); } }); }); 将斜杠添加到每个链接,即使是包含“http://”的链接也不知道为什么?我没有得到任何错误 有没有办法解决这个问题?你

此脚本:

$(function() {
    $('a').each(function() {
        var href = $(this).attr('href');
        if ("a:not(http://)") {
            $(this).attr('href', '/' + href);
        }
    });
});
将斜杠添加到每个链接,即使是包含“http://”的链接也不知道为什么?我没有得到任何错误


有没有办法解决这个问题?

你把两件事搞混了:

  • jQuery选择器:

    $(function() {
        $('a:not([href^="http://"])').each(function() {
            var href = $(this).attr('href');
            $(this).attr('href', '/' + href);  
        });
    });
    
  • 和纯javascript
    if
    语句:

    $('a').each(function() {
       var href = $(this).attr('href');
       if (href.substr(0, 'http://'.length) == 'http://'){
           $(this).attr('href', '/' + href); 
       }   
    });
    
两者的作用相同

请注意,它们将为http以外的其他方案生成无效链接(例如
/https://example.com/index.html
)。根据您使用的HTML代码的干净程度,您可以简单地查找冒号来标识绝对链接:

$(function() {
    $('a:not([href*=":"])').each(function() {
        var href = $(this).attr('href');
        $(this).attr('href', '/' + href);  
    });
});

你混淆了两件事:

  • jQuery选择器:

    $(function() {
        $('a:not([href^="http://"])').each(function() {
            var href = $(this).attr('href');
            $(this).attr('href', '/' + href);  
        });
    });
    
  • 和纯javascript
    if
    语句:

    $('a').each(function() {
       var href = $(this).attr('href');
       if (href.substr(0, 'http://'.length) == 'http://'){
           $(this).attr('href', '/' + href); 
       }   
    });
    
两者的作用相同

请注意,它们将为http以外的其他方案生成无效链接(例如
/https://example.com/index.html
)。根据您使用的HTML代码的干净程度,您可以简单地查找冒号来标识绝对链接:

$(function() {
    $('a:not([href*=":"])').each(function() {
        var href = $(this).attr('href');
        $(this).attr('href', '/' + href);  
    });
});

首先,您的代码等于以下内容

$(function() {
   $('a').each(function() {
      var href = $(this).attr('href');
      if(true) { $(this).attr('href', '/' + href); }   
   });
});
如果您确实希望根据条件更新href,则if statement应不同:

$(function() {
   $('a').each(function() {
     var href = $(this).attr('href');
     if(href.indexOf('http://') == -1 ) { $(this).attr('href', '/' + href); }   
   });
});

另一种方法是@Yogu提供的方法,在这里,你不需要循环你不打算更新的链接。首先,你的代码等于

$(function() {
   $('a').each(function() {
      var href = $(this).attr('href');
      if(true) { $(this).attr('href', '/' + href); }   
   });
});
如果您确实希望根据条件更新href,则if statement应不同:

$(function() {
   $('a').each(function() {
     var href = $(this).attr('href');
     if(href.indexOf('http://') == -1 ) { $(this).attr('href', '/' + href); }   
   });
});
另一种方法是@Yogu提供的方法,你不需要循环寻找你不打算更新的链接,而只是Yogu的一个插件

请记住,在每个标记的上下文中。“在对象本身内部”。 因此,您可以直接访问href

$('a').each(function() {
   if (this.href.substr(0, 'http://'.length) == 'http://'){
       this.setAttribute('href', '/' + href); 
   }   
});
只是瑜珈的一个插件

请记住,在每个标记的上下文中。“在对象本身内部”。 因此,您可以直接访问href

$('a').each(function() {
   if (this.href.substr(0, 'http://'.length) == 'http://'){
       this.setAttribute('href', '/' + href); 
   }   
});

如果(“a:not(http://)”要做什么?为什么要这样做?这将更新相对链接、SSL链接、文件链接等。如果((a:not(http://))要做什么?为什么要这样做?这将更新相对链接、SSL链接、文件链接等。。。