将html代码转换成js字符串的小工具

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
<script> 
var toJs =function(){
var html = document.getElementById("myhtml").value;
var reg=new RegExp("\r\n","g");
html = html.replace(/\'/g,"\\'").replace(/\"/g,'\\"');
html=html.replace(/[\r\n]/g,'\"\+\r\"');
html=html.replace(/\"\s*\"\+/g,'');
html=html.replace(/\"\s+</g,'\"<');
document.getElementById("myjs").value="\""+html+"\"";
}
</script>


</head>
<body>
  <label> HTML  :</label><textarea rows="10" cols="100" id="myhtml"></textarea>
  <div ><span style="color:red">友情提醒:   html文档末尾不要留空白行、回车符</span></div><br/><button onclick="toJs()"  >执行转换</button><textarea rows="10" cols="100" id="myjs"></textarea>
  


</body>


</html>

你可能感兴趣的:(js,html转换js)