我有一个带有排序功能的HTML表,可按列按升序或降序排序。为了表明我正在使用向下 - 向上 - 用十六进制代码x25BE指向小三角形;和x25B4;分别。
问题是我无法使用replace方法替换这些十六进制字符。我只能通过使用如下字符来做到这一点: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://stackoverflow.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>
答案 0 :(得分:0)
正如@Kelvin Sherlock在评论中所写的那样使用\ u25BE起作用:.replace('\ u25BE;','');