我能够动态生成Unicode字符并使用<div>
等序列将其插入ð
,但现在我想将此输入检索为转义序列,而不是字符本身。
<button id="insertDh">insert funny d to mytext</button>
<div id="mytext"><i>mytext: please click button above</i></div>
<hr>
<textarea id="theSource"></textarea>
<button id="getSource">get mytext's source</button>
$("#insertDh").click(function() {
$("#mytext").html("ð");
});
$("#getSource").click(function() {
$("#theSource").val($("#mytext").html());
});
换句话说,当我点击“获取mytext的源代码”时,我想用ð
而不是ð填写textarea。这可能吗?如果是这样,怎么样?
答案 0 :(得分:2)
您可以使用charCodeAt()
获取十进制字符,然后使用toString(16)
将其转换为十六进制,如下所示:
temp = $("#mytext").html().charCodeAt(0).toString(16);
while (temp.length < 4) {
temp = '0'+temp; //complete hex number with zeros to obtain four digits
}
temp = '&#x' + temp + ';';
$("#theSource").val(temp);
见工作demo
答案 1 :(得分:2)
$("#theSource").val(
$("#mytext").html()
// Replace non-ascii code-points with HTML entities.
.replace(
/[\ud800-\udbff][\udc00-\udfff]|[^\x00-\xff]/g,
function (nonAscii) {
var codepoint;
if (nonAscii.length === 1) { // A single basic-plane codepoint.
codepoint = nonAscii.charCodeAt(0);
} else { // A surrogate pair representing a unicode scalar value.
codepoint = 0x10000 + (
((nonAscii.charCodeAt(0) & 0x3ff) << 10)
| (nonAscii.charCodeAt(0) & 0x3ff));
}
return '&#x' + codepoint.toString(16) + ';';
}));