js 倒计时,转义

function leftTimer(time) {
        var leftTime = (new Date(time)) - (new Date()); //计算剩余的毫秒数 
        var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数 
        var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
        var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟 
        var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数 
        days = checkTime(days);
        hours = checkTime(hours);
        minutes = checkTime(minutes);
        seconds = checkTime(seconds);
        return days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
    }

    function checkTime(i) { //将0-9的数字前面加上0,例1变为01 
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }

 

 

js转义和反转义

//HTML反转义
function HTMLDecode(text) { 
var temp = document.createElement("div"); 
temp.innerHTML = text; 
var output = temp.innerText || temp.textContent; 
temp = null; 
return output; 
} 
var tagText = "<p><b>123&456</b></p>";
var encodeText = HTMLEncode(tagText);
console.log(encodeText);//&lt;p&gt;&lt;b&gt;123&amp;456&lt;/b&gt;&lt;/p&gt;
console.log(HTMLDecode(encodeText)); //<p><b>123&456</b></p>



//HTML转义
function HTMLEncode(html) {
var temp = document.createElement("div");
(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
var output = temp.innerHTML;
temp = null;
return output;
}

var tagText = "<p><b>123&456</b></p>";
console.log(HTMLEncode(tagText));//&lt;p&gt;&lt;b&gt;123&amp;456&lt;/b&gt;&lt;/p&gt;

 

posted @ 2018-08-14 15:27 小泉哥哥 阅读(...) 评论(...) 编辑 收藏