简体   繁体   中英

how to replace href url of html tags with incomplete attribute in jquery?

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;
        });
    });

I want to replace all link in html above with http://alfanetcell.hostoi.com/phpwidget/download.php?type=int&link='+url1 when the script that I enter into my blog and then all the links in my blog be changed by the script (i'am sorry, My English is very difficult !)

UPDATE !, thank's for all my friends, I have found the solution of this discussion:

     $("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;
        });
    });

Because your selector a targets all anchor elements in the page,

Instead you can add a class download to those anchro elements you want to change like

<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 />

then

$(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;
    });
});

You can do something more straight-forward like this:

$(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

Note this will cause issues if the link is clicked multiple times because it will take the modified url as the old url, and keep appending to itself each time you click. Not sure what the intended behaviour is so I'll leave that to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM