Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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 使用jquery转换页面加载上的href链接_Javascript_Jquery_Html_Regex - Fatal编程技术网

Javascript 使用jquery转换页面加载上的href链接

Javascript 使用jquery转换页面加载上的href链接,javascript,jquery,html,regex,Javascript,Jquery,Html,Regex,我需要将所有链接(href值)转换为特定格式 例如,如果一个页面有一个链接,我想将其href转换为 下面是我用于此任务的jquery(请注意,我是jquery新手) 但上面的脚本正在转换网站的所有链接,我的导航链接也在改变我们如何筛选以仅更改包含“http”和“www”的链接的href? 我知道某种正则表达式可以用于此目的。提前谢谢 我认为您应该提取所有内部链接 您可以检查值是否符合您的条件(http://www): 但最好提取所有内部链接: var myHost = new RegExp(lo

我需要将所有链接(href值)转换为特定格式

例如,如果一个页面有一个链接,我想将其href转换为

下面是我用于此任务的jquery(请注意,我是jquery新手)

但上面的脚本正在转换网站的所有链接,我的导航链接也在改变我们如何筛选以仅更改包含“http”和“www”的链接的href?


我知道某种正则表达式可以用于此目的。提前谢谢

我认为您应该提取所有内部链接

您可以检查值是否符合您的条件(
http://www
):

但最好提取所有内部链接:

var myHost = new RegExp(location.host);

$('a').each(function(){
   var value = $(this).attr('href');
   if(!(myHost.test(value))){
     $(this).attr('href','http://url.com/xyz?'+value);
   }
});
最后一个更好的选择是只使用外部链接:(如果你不必对内部链接做任何事情)


我认为你应该提取所有的内部链接

您可以检查值是否符合您的条件(
http://www
):

但最好提取所有内部链接:

var myHost = new RegExp(location.host);

$('a').each(function(){
   var value = $(this).attr('href');
   if(!(myHost.test(value))){
     $(this).attr('href','http://url.com/xyz?'+value);
   }
});
最后一个更好的选择是只使用外部链接:(如果你不必对内部链接做任何事情)


您可以检查链接是外部链接还是内部链接,然后可以添加额外的工作

$( document ).ready(function() {  
 $('a').each(function() {
 var value = $(this).attr('href');
  if($(this[href^='http'])
  {
     $(this).attr('href','http://url.com/xyz?'+value);
   }
  });
});

您可以检查链接是外部链接还是内部链接,然后可以添加额外的工作

$( document ).ready(function() {  
 $('a').each(function() {
 var value = $(this).attr('href');
  if($(this[href^='http'])
  {
     $(this).attr('href','http://url.com/xyz?'+value);
   }
  });
});

您可以尝试以下代码

$( document ).ready(function() {   
    $('a').each(function() {
        var value = $(this).attr('href');
        if(value.match("http://www.*"))
        {
            $(this).attr('href','http://url.com/xyz?'+value);
        }
    });
});
您必须确保没有导航链接包含

"http://www" 
在他们里面。使用不想替换href的相对链接


或者,您可以为要相应修改的所有锚定标记指定一个类,您可以尝试以下代码

$( document ).ready(function() {   
    $('a').each(function() {
        var value = $(this).attr('href');
        if(value.match("http://www.*"))
        {
            $(this).attr('href','http://url.com/xyz?'+value);
        }
    });
});
您必须确保没有导航链接包含

"http://www" 
在他们里面。使用不想替换href的相对链接

或者,您可以为所有要相应修改的锚定标记指定一个类

使用元素检查器检查锚定标记的href

使用及

修改自CSS-Tricks.com

使用元素检查器检查锚定标记的href

使用及

修改自CSS-Tricks.com

一种方法:

// selects all 'a' elements:
$('a')
// sets the 'href' attribute, using the anonymous function to iterate over each
// 'a' element:
.attr('href', function(i,oldhref){
    // i:  the index of the current element in the returned collection,
    // oldhref: the value of the 'href' before manipulation by the function.
    // if 'exampledomain.com' features in the hostname of the link we 
    // manipulate the 'href' as required, otherwise we simply set the 'href'
    // to its original value:
    return this.hostname.indexOf('exampledomain.com') > -1 ? 'www.url.com/xyz?' + oldhref : oldhref;
});

参考资料:

  • JavaScript:
  • jQuery:
一种方法:

// selects all 'a' elements:
$('a')
// sets the 'href' attribute, using the anonymous function to iterate over each
// 'a' element:
.attr('href', function(i,oldhref){
    // i:  the index of the current element in the returned collection,
    // oldhref: the value of the 'href' before manipulation by the function.
    // if 'exampledomain.com' features in the hostname of the link we 
    // manipulate the 'href' as required, otherwise we simply set the 'href'
    // to its original value:
    return this.hostname.indexOf('exampledomain.com') > -1 ? 'www.url.com/xyz?' + oldhref : oldhref;
});

参考资料:

  • JavaScript:
  • jQuery:

您应该检查链接是否为外部..您应该检查链接是否为外部。。