javascript keyup事件仅适用于镀铬时间计算器

时间:2013-10-18 11:35:03

标签: javascript javascript-events cross-browser onkeyup

 <script>
    function numeri_validator(t) {
        var patt = /(\d*)\.{1}(\d{0,2})/;
        var donepatt = /^(\d*)\.{1}(\d{2})$/;
        var str = t.value;
        var result;
        if (!str.match(donepatt)) {
            result = str.match(patt);
            if (result != null) {
                t.value = t.value.replace(/[^\d]/gi, '');
                str = result[1] + '.' + result[2];
                t.value = str;
            } else {
                if (t.value.match(/[^\d]/gi))
                    t.value = t.value.replace(/[^\d]/gi, '');
            }
        }



       //get the date
            var val1 = document.getElementById("cost" + 1).value;
            var val2 = document.getElementById("cost" + 2).value;

       //Get date
            var date = document.getElementById("Label1").innerText;

        //format time
            var timeIn = val1.replace('.', ':');
            var timeOut = val2.replace('.', ':');

        //Concat time with date
            var timeinDate = date + " " + timeIn;
            var timeoutDate = date + " " + timeOut;

        //calculate time difference
            var nDifference = Math.abs(new Date(timeoutDate) - new Date(timeinDate));

            var one_hours = 1000 * 60 * 60;
            var hours = (Math.floor(nDifference / one_hours));

            var diff = nDifference % one_hours;

            var one_min = 1000 * 60;

            var diffmin = Math.round(diff / one_min);

            document.getElementById("datelabel").innerText = hours + "." + diffmin;

            document.getElementById("total").value = hours + "." + diffmin;

这个函数计算两次之间的差异工作正常,但仅在其他浏览器的chrome中工作

document.getElementById("total").value=NaN:NaN

请帮帮我

感谢

2 个答案:

答案 0 :(得分:0)

问题应该是因为代码中的这一部分。 .innerText

var date = document.getElementById("Label1").innerText;

.innerText在Mozilla中不起作用。请改用.textContent

var date = document.getElementById("Label1").textContent;

答案 1 :(得分:0)

我通过更改日期格式解决了这个问题。实际上这个问题有新的Date()

新的日期格式应为

new Date('2013/10/19 12:00') //不是2013-10-19

现在正在开发所有浏览器

由于