简体   繁体   English

jQuery用新值替换链接

[英]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. 我编写了一个jquery函数,用一个新值替换链接的href,但是它不起作用。

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. 请参阅第二条评论以获得答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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