function
htmlEncode
(
html
)
{
var
temp
=
document
.
createElement
(
"div"
)
;
(
temp
.
textContent
!=
undefined
)
?
(
temp
.
textContent
=
html
)
:
(
temp
.
innerText
=
html
)
;
var
output
=
temp
.
innerHTML
;
temp
=
null
;
return
output
;
function
htmlDecode
(
text
)
{
var
temp
=
document
.
createElement
(
"div"
)
;
temp
.
innerHTML
=
text
;
var
output
=
temp
.
innerText
||
temp
.
textContent
;
temp
=
null
;
return
output
;
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
;
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...