如何在JavaScript中将DOM节点文本值转换为UTF-8

How to convert DOM node text value to UTF-8 in JavaScript?

本文关键字:文本 转换 UTF-8 节点 DOM JavaScript 中将      更新时间:2023-09-26

我需要通过Ajax请求发送HTML节点的文本内容,但首先将其转换为UTF-8。这可能吗?

谢谢!

function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}
function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}
从http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html

这样用……

encode_utf8(document.getElementById(id).textContent);