【发布时间】:2024-01-23 10:07:01
【问题描述】:
我有一个带有排序功能的 HTML 表格,可以按列按升序或降序排序。为了表明我正在使用向下和向上 用十六进制代码 x25BE 指向小三角形;和 x25B4;分别。
问题是我无法使用替换方法替换这些十六进制字符。我只能通过使用以下字符来做到这一点:mystring.replace('▴','');但这是不可能的,因为我的 javascript 代码是生成的,并且 ▴ 字符不能在生成代码中使用。
我可以使用十进制代码#9662;和#9652; ,如果有帮助的话。有关我尝试过的表达式,请参阅我的 sortTable 函数的代码,包括这篇文章的建议: javascript replaceing special characters
<html>
<meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<head>
<script type="text/javascript">
function sortTable(id,n) {
table = document.getElementById(id);
//I am skipping the sorting here.
//The question is: how to replace hex or dec characters?
var ths = table.getElementsByTagName('TH')
for (i = 0; i < ths.length; i++) {
//Not working
ths[i].innerHTML = ths[i].innerHTML.replace(' ▾','');
ths[i].innerHTML = ths[i].innerHTML.replace(' ▴','');
//Also not working
//https://*.com/questions/4347366/javascript-replaceing-special-characters
ths[i].innerHTML = ths[i].innerHTML.replace(/\x25BE/g,'');
ths[i].innerHTML = ths[i].innerHTML.replace(/[\xAE\xFC]/g,'');
//This works but I cannot use it!
//ths[i].innerHTML = ths[i].innerHTML.replace(/[▴▾]/g,'');
//mimick switching down-pointing small triangle with up-pointing one
if (i == n) {
ths[i].innerHTML = ths[i].innerHTML + ' ▴';
}
}
}
</script>
</head>
<body>
<table id="tableID">
<tbody>
<tr>
<th onclick="sortTable('tableID',0)">Col1 ▾</th>
<th onclick="sortTable('tableID',1)">Column 2 </th>
</tr>
<tr>
<td>A</td>
<td>100</td>
</tr>
<tr>
<td>B</td>
<td>20</td>
</tr>
<tr>
<td>C</td>
<td>50</td>
</tr>
</tbody>
</table>
</body>
</html>
【问题讨论】:
-
尝试“\u25BE”/“\u25B4”
-
好的,谢谢!如果你写一个答案,我会给你信用
标签: javascript replace hex