📜  js 将字符转换为 html - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:32.200000             🧑  作者: Mango

代码示例1
//To do this simply create a element in the DOM tree and 
//set the innerText of the element to your string. 
//Then retrieve the innerHTML of the element. 
//The browser will return an HTML encoded string.

function HtmlEncode(s)
{
  var el = document.createElement("div");
  el.innerText = el.textContent = s;
  s = el.innerHTML;
  return s;
}

console.log(HtmlEncode('&;\'><"'));

//expected output: &;'><"