用jquery替换链接

用jquery替换链接,jquery,html,Jquery,Html,我有一个网站,允许用户提交视频和属性的来源,有时他们会与http://链接,有时只与www。继续链接 出于各种原因,我需要在每个链接上预先添加http://以确保正确解析URL,因此我只运行了一点jquery,在加载页面时检查链接 $(document).ready(function(){ $('.status-contain a.source-container').each(function(){ this.href = this.href.replace('http

我有一个网站,允许用户提交视频和属性的来源,有时他们会与http://链接,有时只与www。继续链接

出于各种原因,我需要在每个链接上预先添加http://以确保正确解析URL,因此我只运行了一点jquery,在加载页面时检查链接

$(document).ready(function(){
    $('.status-contain a.source-container').each(function(){
        this.href = this.href.replace('http://http//', 'http://');

    });
});
当人们提交
https://
的链接时,这就成了一个问题,因为附加的URL将变成
http://https://
jquery无法识别要更改的内容

我是否应该创建某种if/函数来检查URL是http还是https,然后在其上运行替换


简单的问题,但我有点困惑。

试试这个,检查
href
中是否存在
https
,然后相应地替换它,如下所示:

$(document).ready(function(){
    $('.status-contain a.source-container').each(function(){
        var hrefVal = this.href;
        if(hrefVal.indexOf("https://")!=-1)
        {
          this.href = this.href.replace('http://https://', 'https://');
        }
        else
        {
          this.href = this.href.replace('http://http//', 'http://');
        }

    });
});
也要这样做,

 this.href = this.href.replace('http://https//', 'https://');
你的代码变成

 $(document).ready(function(){
$('.status-contain a.source-container').each(function(){
    this.href = this.href.replace('http://http//', 'http://');
    this.href = this.href.replace('http://https//', 'https://');

});
}))

您可以使用添加自定义url验证方法。自定义验证器可以是这样的

jQuery.validator.addMethod("complete_url", function(val, elem) {
// if no url, don't do anything
if (val.length == 0) { return true; }

// if user has not entered http:// https:// or ftp:// assume they mean http://
if(!/^(https?|ftp):///i.test(val)) {
    val = 'http://'+val; // set both the value
    $(elem).val(val); // also update the form element
}
// now check if valid url
// http://docs.jquery.com/Plugins/Validation/Methods/url
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
return /^(https?|ftp)://(((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:)*@)?(((d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5]))|((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?)(:d*)?)(/((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)+(/(([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)*)*)?)?(?((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)|[uE000-uF8FF]|/|?)*)?(#((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)|/|?)*)?$/i.test(val);
}))