我正在尝试替换text,但是我需要保持链接不变。当我替换text时,它剥离了“ a”标签
<th><a href="#">Leave Link</a> Oct 2019 <a href="#">Leave Link</a></th>
jQuery('th').text(function () {
return $(this).text().replace('Oct', 'OCTOBER');
});
当前结果是
<th>Leave Link OCTOBER 2019 Leave Link</th>
需要结果是
<th><a href="#">Leave Link</a> OCTOBER 2019 <a href="#">Leave Link</a></th>
谢谢您的回答,最后,如果我要更改几个文本,可以像这样合并到同一功能中吗?还是我会那样做?
$('#calendar .month_header th').html(function () {
return $(this).html().replace('Oct', 'OCTOBER');
return $(this).html().replace('Nov', 'NOVEMBER');
return $(this).html().replace('Dec', 'DECEMBER');
});
还是这样的唯一选择
jQuery('#calendar .month_header th').html(function () {
return $(this).html().replace('Oct', 'OCTOBER');
});
jQuery('#calendar .month_header th').html(function () {
return $(this).html().replace('Nov', 'NOVEMBER');
});
jQuery('#calendar .month_header th').html(function () {
return $(this).html().replace('Dec', 'DECEMBER');
});
只需使用.html而不是文本
const months = {
"Oct": "OCTOBER",
"Nov": "NOVEMBER",
"Dec": "DECEMBER"
}
$('th').html(function() {
const month = $(this).text().match(/(\w{3}) \d{4}/)[1];
return $(this).html().replace(month, months[month]);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th><a href="#">Leave Link</a> Oct 2019 <a href="#">Leave Link</a></th>
<th><a href="#">Leave Link</a> Nov 2019 <a href="#">Leave Link</a></th>
<th><a href="#">Leave Link</a> Dec 2019 <a href="#">Leave Link</a></th>
</tr>
</thead>
</table>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句