当字符串中包含html标签,怎么避免标签匹配混乱? 100分,谢谢

spiderboy 2005-02-01 01:07:37
<script language='javascript'>

function preview(str){

xxWin=window.open('','newwindow','height=450,width=550,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
xxWin.document.write(str);
xxWin.document.close() ;

}

</script>

当str变量中的字符串包含html标签的时候,会产生标签匹配混乱的问题,用replace()也不能很好的解决.怎么做?????????
...全文
178 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
seeu1688 2005-02-01
  • 打赏
  • 举报
回复
换种思路:
用<xmp></xmp>按文本方式显示内容


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="Seagle.K">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<script language='javascript'>
function preview(str){
xxWin=window.open('','newwindow','height=450,width=550,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
xxWin.document.open("text/txt");
xxWin.document.write(str);
xxWin.document.close() ;
}

var a = "<body><b><font color=red>\nhello world</font></b><table border=1><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table></body>";

a = "<xmp>" + a + "</xmp>";
preview(a);
</script>
</body>
</html>
SuperBJack 2005-02-01
  • 打赏
  • 举报
回复
function CheckStr(str)
str = trim(str)
str = replace(str, ">", ">")
str = replace(str, "<", "<")
str = Replace(str, CHR(32), " ")
str = Replace(str, CHR(9), " ")
str = Replace(str, " ", " ")
str = Replace(str, CHR(34), """)
str = Replace(str, CHR(39), "'")
str = Replace(str, vbCr, "")
str = Replace(str, vbLf, "<BR>")
str = Replace(str, vbCrLf, "<BR>")
CheckStr = str
end function
8988 2005-02-01
  • 打赏
  • 举报
回复
用下面函数处理一下str

function HTMLEncode(str){
str=str.replace(/&/g,"&");
str=str.replace(/</g,"<");
str=str.replace(/>/g,">");
str=str.replace(/\n/g,"<br>");
str=str.replace(/\"/g,""");
return str;
}
ttyp 2005-02-01
  • 打赏
  • 举报
回复
str=str.replace(/&/g,"&");
str=str.replace(/</g,"<");
str=str.replace(/>/g,">");

87,922

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧