javascript教程 html非法字符过滤与转换函数
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<script language="javascript">
function htmlspecialchars(str) {
return preg_replace(['&', '<', '>', '"'], ['&', '<', '>', '"'], str);
}
function enhtml(str)
{
return preg_replace([' ', '<', '>', '&'], [' ', '<', '>', '&'], str);
}
function htmltohtml()
{
$("t2").value =htmlspecialchars($("t1").value);
}
function htmltoA()
{
$("t1").value =htmlspecialchars($("t2").value);
}
function $(id)
{
return document.getElementById(id);
}
</script>
</head>
<body>
<p> </p>
<form id="form1" name="form1" method="post" action="">
<label>
<textarea name="t1" cols="" rows="" id="t1"></textarea>
<br />
<br />
<input type="button" name="Submit" value="转换成html编码" onclick="htmltohtml();" />
</label>
<label>
<input type="button" name="Submit2" value="转换成html标签" onClick="htmltoA();" />
</label>
<p>
<textarea name="t2" cols="" rows="" id="t2"></textarea>
</p>
</form>
</body>
</html>