Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 附加“;www";在使用Jquery的html文档中的每个url之前_Javascript_Jquery_Url - Fatal编程技术网

Javascript 附加“;www";在使用Jquery的html文档中的每个url之前

Javascript 附加“;www";在使用Jquery的html文档中的每个url之前,javascript,jquery,url,Javascript,Jquery,Url,我需要使用jquery在任何锚定标记或任何表单操作[即,DOM中的任何链接]中的每个url[如果还没有]之前添加www。可能吗 这不适合http://(和其他变体),但您可以这样做 $("a").each(function(){ var href = $(this).attr("href"); if(href.indexOf("www.") != 0){ $(this).attr("href", "www." + href); } }); form标记使用了act

我需要使用jquery在任何锚定标记或任何表单操作[即,DOM中的任何链接]中的每个url[如果还没有]之前添加www。可能吗

这不适合
http://
(和其他变体),但您可以这样做

$("a").each(function(){
   var href = $(this).attr("href");
   if(href.indexOf("www.") != 0){
      $(this).attr("href", "www." + href);
   }
});
form
标记使用了
action
属性,因此,如果您也需要这样做,则必须执行一些额外的代码。检查标记名,或者仅为表单创建一个新循环。大概是这样的:

$("a, form").each(function () {
    var attr = $(this).prop("tagName") == "FORM" ? "action" : "href";
    var href = $(this).attr(attr);
    if (href.indexOf("www.") != 0) {
        $(this).attr(attr, "www." + href);
    }
});

注意:这只是一个例子。有很多事情要考虑。我已经提到了方案问题(HTTP等),然后你必须考虑相对路径(即“/Frase/Page .html”)。一切都变得有点混乱,你确定这是你真正需要做的吗

$('a').each(function(){
        if($(this).attr('href').indexOf('www')!=0){
            $(this).attr('href', 'www.'+$(this).attr('href'));
        }
   });

是的,这是可能的。你能发布你解决这个问题的尝试吗?谢谢你的回答。不,没有相对路径。也没有https。。只有http。。。做了之后。。。对代码进行一些修改,它可以完美地工作。所以再次感谢:)