JS在html中的字符串替换

html-css032

JS在html中的字符串替换,第1张

上面font标签加一个id,<font id="idFont" class="cfrq" style="font-size:12px">星期三,星期二</font>

这样在js中就可以

docunment.getElementById("idFont").innerText = "星期二,星期三"

这样就可以实现你想要的功能

如果你用JQuery的话也可以这样 $(".cfrq").text("星期二,星期三")

asp用Server.HTMLEncode

<%response.write(Server.HTMLEncode("The image tag: <img>"))%>

javascript 用正则表达式进行转换处理

var HtmlUtil = {

/*1.用正则表达式实现html转码*/

htmlEncodeByRegExp:function (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

},

/*2.用正则表达式实现html解码*/

htmlDecodeByRegExp:function (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

}

}