如何在jquery中用不完整的属性替换html标签的href url?

时间:2015-03-12 03:13:16

标签: javascript jquery html

HTML:

<p><a href='http://download.onlagump3.com/bokep.php?search=Kehna+Hi+Kya+++Drishti+Garg' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 1</font></a></p>
<br />      <p><a href='http://www.myfreemp3.cc/mp3/Kehna+Hi+Kya+++Drishti+Garg' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 2</font></a></p>
<br />      <p><a href='http://mp3skull.com/mp3/Kehna_Hi_Kya___Drishti_Garg.html' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 3</font></a></p>
<br />      <p><a href='http://www.stafaband.info/download/mp3/lagu_Kehna_Hi_Kya___Drishti_Garg/' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 4</font></a></p>
<br />      <p><a href='https://www.google.com/#q=site:4shared.com+Kehna+Hi+Kya+++Drishti+Garg' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 5</font></a></p>
<br />

JQUERY:

 $(document).ready(function() {
         $("a").click(function(e){
           var url1 = $(this).attr('href');
           var url = url1.replace(url1,'http://alfanetcell.hostoi.com/phpwidget/download.php?type=int&link='+url1);
           //$('#result').replaceWith( "<div id='result'>" + url + "</div>" );
var newurl = $('a[href="'+url1+'"]').attr("href", url);
             $('a[href="'+url1+'"]').$(this).attr("target", "_blank");
           return false;
        });
    });

我想用http://alfanetcell.hostoi.com/phpwidget/download.php?type=int&link='+url1替换上面html中的所有链接,当我输入我的博客的脚本然后我的博客中的所有链接被脚本更改时(对不起,我的英语非常好)很难!)

更新!,感谢所有朋友,我找到了这个讨论的解决方案:

     $("p a").click(function(e){
           var url1 = $(this).attr('href');
           var url = url1.replace(url1,'http://alfanetcell.hostoi.com/phpwidget/download.php?type=int&link='+url1);
           //$('#result').replaceWith( "<div id='result'>" + url + "</div>" );
var newurl = $('a[href="'+url1+'"]').attr("href", url);
             $('a[href="'+url1+'"]').$(this).attr("target", "_blank");
           return false;
        });
    });

2 个答案:

答案 0 :(得分:0)

因为您的选择器a定位页面中的所有锚元素,

相反,您可以将类download添加到要更改的anchro元素

<p><a class="download" href='http://download.onlagump3.com/bokep.php?search=Kehna+Hi+Kya+++Drishti+Garg' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 1</font></a></p>
<br />      
<p><a class="download" href='http://www.myfreemp3.cc/mp3/Kehna+Hi+Kya+++Drishti+Garg' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 2</font></a></p>
<br />      
<p><a class="download" href='http://mp3skull.com/mp3/Kehna_Hi_Kya___Drishti_Garg.html' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 3</font></a></p>
<br />      
<p><a class="download" href='http://www.stafaband.info/download/mp3/lagu_Kehna_Hi_Kya___Drishti_Garg/' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 4</font></a></p>
<br />      
<p><a class="download" href='https://www.google.com/#q=site:4shared.com+Kehna+Hi+Kya+++Drishti+Garg' target='_blank'><font color='blue' size='4'>DOWNLOAD From Server 5</font></a></p>
<br />

然后

$(document).ready(function() {
    $(".download a").click(function(e){
        $(this).attr('href', 'http://alfanetcell.hostoi.com/phpwidget/download.php?type=int&link='+$(this).attr('href')).attr("target", "_blank");
        return false;
    });
});

答案 1 :(得分:0)

你可以像这样直截了当地做点什么:

$(document).ready(function() {
  $("a").click(function(e){
    var old_url = $(this).attr('href');
    var new_url = 'http://alfanetcell.hostoi.com/phpwidget/download.php?type=int&link=' + old_url;
    $(this).attr('href', new_url);
    return true;
 });
});

JSFiddle

请注意,如果多次点击该链接会导致问题,因为它会将修改后的网址作为旧网址,并且每次点击时都会自动附加。不确定预期的行为是什么,所以我会把它留给你。