请问各位怎么利用js实现文字与背景反色?

JavaScript06

请问各位怎么利用js实现文字与背景反色?,第1张

<div id='d1' style='color:#336699font-size:40px'>

xxxx

</div>

<input type='button' onclick="test()" value='设置反色背景' />

<script>

function oppositeColor(a){

a=a.replace('#','')

var c16,c10,max16=15,b=[]

for(var i=0i<a.lengthi++){

c16=parseInt(a.charAt(i),16)//  to 16进制

c10=parseInt(max16-c16,10)// 10进制计算

b.push(c10.toString(16)) // to 16进制

}

return '#'+b.join('')

}

function test(){

var d=document.getElementById("d1")

//alert(oppositeColor(d.style.color))

d.style.background=oppositeColor(d.style.color)

}

</script>

(1)板材含水率过高,日久水份挥发积留于漆膜中导致发白;(2)环境湿度过高;(3)施工表面、容器、油漆中混有水分;(4)稀释剂挥发太快;(5)底层漆膜中含有的水分没有清除干净;(6)对于黑胡桃木等深色板材使用透明性较差的油漆,因为透明性问题发白;(7)油漆施工过厚;(8)固化剂配套错误,与油漆不相容而发白。外墙涂料刷过一边后墙面出现大面积发白现象是什么原因,有何措施不就 一是涂料遮盖力不好,刷一便不能完全盖底,透底子了。这种涂料多涂几次,直至遮盖住底白色为止,找边处试涂确定涂刷几便。二是底层封闭底漆没做好,水泥层往外反碱(白色),这样面漆啥样也会时有发白现象。

/*1.用浏览器内部转换器实现html编码(转义)*/

    htmlEncode : function (html){ 

            //.首先动态创建一个容器标签元素,如DIV 

            var  temp = document.createElement ("div")

            //然后将要转换的字符串设置为这个元素的innerText或者textContent 

            (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html) 

            //最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了 

            var   output = temp.innerHTML

            temp = null    

            return  output

     },

/*2.用浏览器内部转换器实现html解码(反转义)*/

     htmlDecode : function (text){

            //首先动态创建一个容器标签元素,如DIV

            var  temp =  document.createElement("div")

            //.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)

            temp.innerHTML = text

            //最后返回这个元素的innerText或者textContent,即得到经过HTML解码的字符串了。                    var  output = temp.innerText  ||  temp.textContent

            temp =null

            return  output

     },

注:这两种方法都是利用innerHTML会编译字符串来实现转义和反转义

/*3.用正则表达式实现html编码(转义)*/

     htmlEncodeByRegExp : function (str){ 

            var  temp = " "

            if(str.length == 0)

                return " "

            temp = str.replace(/&/g,"&")

            temp = temp.replace(//g,">")

            temp = temp.replace(/\s/g," ")

            temp = temp.replace(/\'/g,"'")

            temp = temp.replace(/\"/g,""")

            return  temp

     },

/*4.用正则表达式实现html解码(反转义)*/

    htmlDecodeByRegExp:function (str){ 

            var  temp = ""

            if (str.length == 0)

               return  " "

            temp = str.replace(/&/g,"&")

            temp = temp.replace(/</g,"<")

            temp = temp.replace(/>/g,">")

            temp = temp.replace(/ /g," ")

            temp = temp.replace(/'/g,"\'")

            temp = temp.replace(/"/g,"\"")

            return temp 

    },

/*5.用正则表达式实现html编码(转义)(另一种写法)*/

        html2Escape : function(sHtml) {

                return   sHtml.replace (/[<>&"]/g,function(c){return{'<':'<','>':'>','&':'&','"':'"'}[c]})

         },

/*6.用正则表达式实现html解码(反转义)(另一种写法)*/

        escape2Html : function (str) {

                var  arrEntities = {'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}

                return   str.replace(/&(lt|gt|nbsp|amp|quot)/ig,function(all,t){return arrEntities[t]})

        }

原文地址: https://www.cnblogs.com/willingtolove/p/11059325.html