我想用addEventListener复制它

时间:2012-09-06 21:07:48

标签: javascript addeventlistener

我很抱歉,如果这个问题可能有点广泛,但是我已经尝试了所有的东西而且我不能带来替换它的东西(这不是我的,我只是用它):

    function onlyNum(evt){

        evt = window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57)){
            return false;
        }

        return true;
    }

// this INPUT will not accept anything but numbers now.
<input type="text" id="idInput" onKeyPress="return onlyNum(event)">

用这个:

    function onlyNum(evt){

    evt = window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        return false;
    }

    return true;
}

// this INPUT's with this event listeners will just acept numbers now.
// like this code is cleaner since you just need to make a list of ids that will have this listeners.
var input = document.getElementById('idTest');
input.addEventListener('keyDown', onlyNum, false);

我找不到将事件传递给函数和“返回”部分的方法。对不起,我甚至不知道如何描述它,也许这就是为什么我找不到关于这个问题的任何信息......

2 个答案:

答案 0 :(得分:1)

使用addEventListener()attacheEvent()的通用事件处理程序时,如果将事件处理程序放在HTML中,则不能像return false那样阻止默认操作。相反,您需要在大多数浏览器中调用evt.preventDefault()evt.stopPropagation(),或者在旧版本的IE中设置window.event.returnValue = falsewindow.event.cancelBubble = true

为了简单地跨平台,我开发了一个addEvent()函数,可以为您完成所有跨平台工作(包括IE6,IE7,IE8)。因此,使用它来添加事件处理程序,您只需这样做:

addEvent("idTest", "keydown", onlyNum);​

工作演示:http://jsfiddle.net/jfriend00/2ATmz/

以下是使用新事件功能的代码:

function onlyNum(evt){
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        return false;
    }
    return true;
}

addEvent("test", "keydown", onlyNum);​

而且,这是跨平台事件功能:

// refined add event cross browser
function addEvent(elem, event, fn) {
    if (typeof elem === "string") {
        elem = document.getElementById(elem);
    }


    // avoid memory overhead of new anonymous functions for every event handler that's installed
    // by using local functions
    function listenHandler(e) {
        var ret = fn.apply(this, arguments);
        if (ret === false) {
            e.stopPropagation();
            e.preventDefault();
        }
        return(ret);
    }

    function attachHandler() {
        // set the this pointer same as addEventListener when fn is called
        // and make sure the event is passed to the fn also so that works the same too
        var ret = fn.call(elem, window.event);   
        if (ret === false) {
            window.event.returnValue = false;
            window.event.cancelBubble = true;
        }
        return(ret);
    }

    if (elem.addEventListener) {
        elem.addEventListener(event, listenHandler, false);
    } else {
        elem.attachEvent("on" + event, attachHandler);
    }
}

注意:我稍微修改了onlyNum(),因为此事件处理函数总是传递事件对象(您不必为IE处理不同的事情)。

答案 1 :(得分:0)

你可以试试这个

window.onload=function(){ 
    var input = document.getElementById('idTest');
    if(input.addEventListener) input.addEventListener('keydown', onlyNum, false);
    else input.attachEvent('onkeydown', onlyNum, false);
}

function onlyNum(e)
{
    var e=e || window.event;
    var charCode = (e.which) ? e.which : e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        if(e.preventDefault) e.preventDefault();
        else e.returnValue = false;    
    }
}

DEMO