Jquery 用新值替换 link
Jquery replace link with new value
我正在从数据库中读取数据并将每个 link 添加到一个新的 table 行中。
我写了一个 jquery 函数来用新值替换 link 的 href,但它不起作用。
我还必须提到,每次将新的 link 添加到 table 时,我希望立即完成替换,而无需点击事件或其他东西。
$(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>
[已编辑:为清楚起见删除了原始评论]
答案见第二条评论
您的代码包含不必要的行。但是修复已完成
$(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);
});
});
我正在从数据库中读取数据并将每个 link 添加到一个新的 table 行中。 我写了一个 jquery 函数来用新值替换 link 的 href,但它不起作用。
我还必须提到,每次将新的 link 添加到 table 时,我希望立即完成替换,而无需点击事件或其他东西。
$(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>
[已编辑:为清楚起见删除了原始评论]
答案见第二条评论
您的代码包含不必要的行。但是修复已完成
$(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);
});
});