谁能帮我看看这段代码,关于JS获取当前时间的问题的。谢谢

JavaScript07

谁能帮我看看这段代码,关于JS获取当前时间的问题的。谢谢,第1张

<html>

<head>

<script type="text/javascript">

alert(decodeURI('\u6d4f\u89c8\u65e0\u6548\uff0c\u8bf7\u5237\u65b0\u7f51\u9875\u540e\u7ee7\u7eed\u6d4f\u89c8\uff01'))

alert(decodeURI('\u540c\u4e00\u65f6\u95f4\u4e0d\u53ef\u4ee5\u6d4f\u89c8\u591a\u4e2a\u7f51\u9875\uff01'))

alert(decodeURI('\u6d4f\u89c8\u5b8c\u6210\uff01'))

alert(decodeURI('\u6b63\u5728\u6253\u5f00\u7f51\u9875\uff0e\uff0e\uff0e'))

</script>

</head>

<body>

</body>

</html>

你先打开上面这个文件吧。看看你就知道对应的中文意思了

js对中文和特殊字符可以编码,有三个函数escape,encodeURI,encodeURIComponent

对应的解码函数为unescape,decodeURI,decodeURIComponent

这应该不算加密,中文字符在网络传递时,如果不处理的话,容易乱码。

如果我答得好请给我一点分

在html5中document新增了一个事件 visibilitychange,这个事件在页面前台或后台切换时被触发,你所说的问题就简单了,它也有个对应的属性visibilityState,用于检测当前页面的状态值为hidden还是visible。

解法是,在hidden时记录当前时间,在visible时用当前时间减去之前记录的时间就为当前倒计时需要减去的时间,这也就不需要和后台沟通了,或者你直接在visible时刷新页面也行,下面是小样,你测试一下

var b=getTime()

function getTime()

{

return Date.now()

}

document.addEventListener('webkitvisibilitychange',function()

{

if(document.webkitVisibilityState=='hidden')

{

b=getTime()

}else

{

document.body.appendChild(document.createTextNode('间隔:'+(getTime()-b)))

}

})

document.addEventListener('mozvisibilitychange',function()

{

if(document.mozVisibilityState=='hidden')

{

b=getTime()

}else

{

document.body.appendChild(document.createTextNode('间隔:'+(getTime()-b)))

}

})

<!DOCTYPE HTML>

<html>

<head>

    <meta charset="utf-8">

    <style>

        * {

    margin: 0

    padding: 0

}

#main {

    margin: 100px auto

    width: 700px

}

#box {

    margin: 10px

}

#box li {

    width: 300px

    list-style: none

}

#box .title {

    background: #F09

    text-align: center

}

#box .content {

    border: 1px solid #0FC

    background: #CFF

    padding: 10px

    width: 278px

    height: 80px

}

#box li  .input {

    position: relative

    left: 286px

    top: -20px

    width: 14px

}

    </style>

    <script>

        window.onload = function() {

            var oBox = document.getElementById('box')

            var oTitle = document.getElementById('title')

            var oContent = document.getElementById('content')

            var oEnter = document.getElementById('enter')

            function createLi() {

                var oLi = document.createElement('li')

                var oLis = oBox.getElementsByTagName('li')

                var oDiv_title = document.createElement('div')

                var oDiv_content = document.createElement('div')

                var oBtu = document.createElement('input')

                oBtu.className = 'input'

                oDiv_title.className = 'title'

                oDiv_content.className = 'content'

                oBtu.type = 'button'

                oBtu.value = 'X'

                oBtu.onclick = function() {

                    del(this)

                }

                oDiv_title.innerHTML = '<h3>' + oTitle.value + '</h3>'

                oDiv_content.innerHTML = oContent.value

                oLi.appendChild(oDiv_title)

                oLi.appendChild(oDiv_content)

                oLi.appendChild(oBtu)

                if (oLis.length > 0) {

                    oBox.insertBefore(oLi, oLis[0])

                } else {

                    oBox.appendChild(oLi)

                }

            }

            oEnter.onclick = createLi

        }

        var del = function(btn) {

            btn.parentElement.outerHTML = ""

        }

    </script>

</head>

<body>

    <div id="main">

        <label>标题:</label>

        <input id="title" type="text" />

        <label>内容:</label>

        <input id="content" type="text" />

        <input id="enter" type="button" value="确定" />

        <ul id="box">

            <li>

                <div class="title">title</div>

                <div class="content">content</div>

                <input class="input" type="button" value="X" onclick="del(this)" />

            </li>

        </ul>

    </div>

</body>

</html>