/// <summary> /// 清除文本中Html的标签 /// </summary> /// <param name="Content"></param> /// <returns></returns> protected string ClearHtml(string Content) { Content = Zxj_ReplaceHtml("&#[^>]*;", "", Content); Content = Zxj_ReplaceHtml("</?marquee[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?object[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?param[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?embed[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?table[^>]*>", "", Content); Content = Zxj_ReplaceHtml(" ", "", Content); Content = Zxj_ReplaceHtml("</?tr[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?th[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?p[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?a[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?img[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?tbody[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?li[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?span[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?div[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?th[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?td[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?script[^>]*>", "", Content); Content = Zxj_ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content); Content = Zxj_ReplaceHtml("on(mouse|exit|error|click|key)", "", Content); Content = Zxj_ReplaceHtml("<\\?xml[^>]*>", "", Content); Content = Zxj_ReplaceHtml("<\\/?[a-z]+:[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?font[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?b[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?u[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?i[^>]*>", "", Content); Content = Zxj_ReplaceHtml("</?strong[^>]*>", "", Content); string clearHtml = Content; return clearHtml; }
为何要把文字内的html标签去掉呢,是因为在绑定的时候,浏览器会把标签的释义,释义之后会有可能将我们原有的布局打乱。所以,当只展示文字的时候,我们就要这样去做了。遇到这个问题是,在我绑定产品详情的时候遇到的,超过一定字数就截取字符,可以这样又会造成html标签缺省或配合混乱。