相关文章推荐
笑点低的肉夹馍  ·  動物排序 - iT ...·  1 年前    · 
稳重的刺猬  ·  pktgen-dpdk ...·  1 年前    · 
含蓄的书签  ·  SPSS ...·  1 年前    · 
飘逸的打火机  ·  pyspark: ...·  2 年前    · 
忐忑的豆芽  ·  Activity.OnBackPressed ...·  2 年前    · 
function htmlEncode ( html ) { //1.首先动态创建一个容器标签元素,如DIV var temp = document . createElement ( "div" ) ; //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持) ( temp . textContent != undefined ) ? ( temp . textContent = html ) : ( temp . innerText = html ) ; //3.最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了 var output = temp . innerHTML ; temp = null ; return output ; /*2.用浏览器内部转换器实现html解码*/ function htmlDecode ( text ) { //1.首先动态创建一个容器标签元素,如DIV var temp = document . createElement ( "div" ) ; //2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持) temp . innerHTML = text ; //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。 var output = temp . innerText || temp . textContent ; temp = null ; return output ; /*3.用正则表达式实现html转码 后台可用 (保存html到数据库)*/ function htmlEncodeByRegExp ( str ) { var s = "" ; if ( str . length == 0 ) return "" ; s = str . replace ( /&/g , "&" ) ; s = s . replace ( / s = s . replace ( />/g , ">" ) ; s = s . replace ( / /g , " " ) ; s = s . replace ( /\'/g , "'" ) ; s = s . replace ( /\"/g , "" " ) ; return s ; /*4.用正则表达式实现html解码 后台可用 (保存html到数据库)*/ function htmlDecodeByRegExp ( str ) { var s = "" ; if ( str . length == 0 ) return "" ; s = str . replace ( /&/g , "&" ) ; s = s . replace ( /</g , "<" ) ; s = s . replace ( />/g , ">" ) ; s = s . replace ( / /g , " " ) ; s = s . replace ( /'/g , "\'" ) ; s = s . replace ( /"/g , "\"" ) ; return s ; `` `` /*1.用浏览器内部转换器实现html转码*/ function htmlEncode(html){ //1.首先动态创建一个容器标签元素,如DIV var temp = document.createElement ("div"); //2.然后将要转换的字符串设置为这个元素的innerText(ie支持...
代码如下:// Html 结构转 字符串 形式显示 支持<br>换行 function To Html String ( html Str) { return toTXT( html Str).replace(/\<\;br[\ \;|\ \;]*[\/]?\>\;|\r\n|\n/g, “<br>”); } // Html 结构转 字符串 形式显示 function toTXT(str) { var RexStr = /\<|\>|\”|\’|\&| | /g str = str.replace(RexStr, function (MatchStr) { switch (MatchStr) {
2."" + value 3. String (value) 第一种方法存在的问题是,它不能把null和undefined转换为 字符串 .还有第二种和第三种方法,这两种方法的效果基本一样. “”+value: 使用加法运算符配合一个空 字符串 可以把任意值转换为 字符串 ,我觉得这种方法代码的可读性很差,但相对 String (value)来,还是有一些人更喜欢用这种转换方式. String (value): 这种方法可读性更好,唯一的问题是,这种函数调用可能会迷惑一些人,尤其是那些熟悉J
JS 中,有四种基本数据类型,分别为 string 、number、Boolean、undefined;以及一种引用数据类型object 有时需要将其他类型转换为 string ,方法有三种。 第一种方法:使用to string ()方法 使用方法如下: <script type="text/javascript"> var num = 123; num = num.toStrin...