Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查href属性并在需要时添加完整的url地址?_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 如何检查href属性并在需要时添加完整的url地址?

Javascript 如何检查href属性并在需要时添加完整的url地址?,javascript,jquery,regex,Javascript,Jquery,Regex,我想检查href属性,如果它不包含完整路径,我想用完整URL替换它?我应该使用JavaScript吗?它可以做到这一点,但不适用于IE8 我的JS: fullUrl = $(this).filter('[href^='+document.location.protocol+']').attr('href') ? true : false; url = fullUrl ? $(this).filter('[href^='+document.location.protocol+

我想检查href属性,如果它不包含完整路径,我想用完整URL替换它?我应该使用JavaScript吗?它可以做到这一点,但不适用于IE8

我的JS:

fullUrl = $(this).filter('[href^='+document.location.protocol+']').attr('href') 
    ? true : false;

url = fullUrl ? 
    $(this).filter('[href^='+document.location.protocol+']').attr('href') 
    : document.location.protocol + '//' + document.domain + $(this).attr('href');
我需要检查href是否包含完整url:

href:“/path/other.html”(部分)

href:http://domain.com/path/other.html“(完整url)

然后,如果我有部分url href,我必须添加域并将href重新创建为完整url

我想你想要这个:

$(this).attr('href', function(i, v) {
    if ( v.indexOf('http:') === -1 ) {
        v = document.location.protocol + '//' + document.domain + '/' + v; 
    }
    return v;
});

现场演示:

我认为您在多个项目上调用
attr
不会得到有意义的结果。您应该循环检查/更换,一次一项:

$(this).filter('[href^='+document.location.protocol+']').each(function(){
    var fullUrl = $(this).attr('href') ? true : false;
    var url = fullUrl ? $(this).attr('href') : document.location.protocol + '//' + document.domain + $(this).attr('href');
    alert(url);
});

完整的url可通过
.href
属性获得

通常无需将其设置为属性,但如果愿意,可以执行以下操作:

$('a[href]').attr('href', function() { return this.href; });
它通过将函数作为第二个参数传递来查找所有
方法,该参数返回
.href
属性的值


如果您只是想要它们的列表,可以使用该方法

示例:


编辑:

如果要显式检查路径是否已经是完整路径,只需在我的第一个示例中添加
If
语句

$('a[href]').attr('href', function(i,hrf) { 
    if( hrf.indexOf( 'http' ) !== 0 ) return this.href; 
});

此解决方案将确保页面上的所有锚都在href:

$(document).ready(function()
{  
    var str_len = document.location.protocol.length;
    $('a').each(function()
    {
        if($(this).attr('href').substring(0,str_len) != document.location.protocol)
        {
            $(this).attr('href',document.location.protocol + '//' + $(this).attr('href'));
        }
    });
});

您的代码没有替换
href
属性…可能我的问题不正确!抱歉:(.让我再次解释:@Faraona:看看你的更新,我的第一个解决方案似乎可以工作,只是它没有检查当前的
href
。如果你真的需要检查,我会提供一个更新。
$(document).ready(function()
{  
    var str_len = document.location.protocol.length;
    $('a').each(function()
    {
        if($(this).attr('href').substring(0,str_len) != document.location.protocol)
        {
            $(this).attr('href',document.location.protocol + '//' + $(this).attr('href'));
        }
    });
});