删除 td 中的垂直垫片
Remove the vertical spacers in a td
使用 jQuery 从此 td 中删除最后一次出现的元素间隔符 (|) 的最佳方法是什么?
<td class="actionCol" nowrap=""><a href="#xyz">XYZ</a> | <a href="#err">ERR</a> | <a href="#PING">PING</a></td>
您可以使用 substr
替换最后一次出现的内容。无需使用 regex
,这会比 regex
.
更快
$('.actionCol').html(function(i, e) {
var lastIndex = e.lastIndexOf('|');
return e.substr(0, lastIndex) + e.substr(lastIndex + 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<td class="actionCol" nowrap=""><a href="#xyz">XYZ</a> | <a href="#err">ERR</a> | <a href="#PING">PING</a>
</td>
</table>
因此,要仅删除最后一次出现的内容,您可以执行以下操作。
$('.actionCol').html(function(_, elem) {
return elem.replace(/\|(?!.*\|)/, '');
});
使用 jQuery 从此 td 中删除最后一次出现的元素间隔符 (|) 的最佳方法是什么?
<td class="actionCol" nowrap=""><a href="#xyz">XYZ</a> | <a href="#err">ERR</a> | <a href="#PING">PING</a></td>
您可以使用 substr
替换最后一次出现的内容。无需使用 regex
,这会比 regex
.
$('.actionCol').html(function(i, e) {
var lastIndex = e.lastIndexOf('|');
return e.substr(0, lastIndex) + e.substr(lastIndex + 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<td class="actionCol" nowrap=""><a href="#xyz">XYZ</a> | <a href="#err">ERR</a> | <a href="#PING">PING</a>
</td>
</table>
因此,要仅删除最后一次出现的内容,您可以执行以下操作。
$('.actionCol').html(function(_, elem) {
return elem.replace(/\|(?!.*\|)/, '');
});