简体   繁体   中英

Jquery replace link with new value

I am reading data from a database and adding each link in a new table row. I wrote a jquery function to replace the href of the link with a new value, but it is not working.

Also I have to mention that each time a new link is added to the table, I want the replacement to be done instantly, without click event or something.

 $(document).ready(function() { $("a").show(function() { var before = $(this).href; var replacewith = "https://www.google.com" var after = before.replace(before, replacewith); $(this).attr("href", after); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tr> <td> <a href="aaa">Link1</a> </td> </tr> 

Your code has unnecessary lines. However fix is done

$(document).ready(function () {
        $("a").show(function () {
            var before = $(this).attr('href'); 
            var replacewith = "https://www.google.com";
            var after = before.replace(before, replacewith);
            $(this).attr("href", after);
        });
    });

[ edited: removed original comment for clarity ]

See the second comment for answer.

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