开发者

Trouble in Javascript event handling

Only today I figured out how event propagation works and set out pompously to test it on my existing code base but arghhhhhhh damn you javascript......nothing seems to be simple enough with you :X

Here is my problem, I define a set of events on an anchor:

theLink.setAttribute('onMouseOver','doSomething(this)');    **// works**

theLink.addEventListener("mouseout", function(event){doSomethingElse(event)}, false);  **// does not work**

theLink.onmouseout = function(event){doSomethingElse(event)};     **// does not work**

Only if I define events as in the first example then it seems to be working in the second or the third definitions as well. But I can not use that definition because I have to pass event object.

Any hints? 开发者_StackOverflowI am using firefox.


All three of these worked for me using the following code (in firefox):

HTML:

<a id="link1">Link 1</a>
<a id="link2">Link 2</a>
<a id="link3">Link 3</a>

JS:

var link1 = document.getElementById("link1");
var link2 = document.getElementById("link2");
var link3 = document.getElementById("link3");

window.doSomething = function(event) {
    console.log(event);
}

link1.setAttribute('onMouseOver', 'doSomething(this)');

link2.addEventListener("mouseout", function(event) {
    doSomething(event)
}, false);

link3.onmouseout = function(event) {
    doSomething(event)
};

Here is a jsfiddle with it working: http://jsfiddle.net/magicaj/qk6wU/

You might also consider using a library like jQuery that handles cross browser incompatibility with the addEventListener method that is not supported by some versions of IE, the JS would look something like this:

$("#link1").mouseover(doSomething);
$("#link2").mouseover(doSomething);
$("#link3").mouseover(doSomething);


An answer with cross browserness included

function doSomething( event ) {
    if( console && console.log ) {
        console.log( this );
        console.log( event );
    }
    else {
        alert( this === window ? 'WINDOW' : this.tagName );
        alert( event );
    }
}


var link1 = document.getElementById("link1");
var link2 = document.getElementById("link2");
var link3 = document.getElementById("link3");

// `this` within your handler will be `window` and not the link
link1.setAttribute( 'onMouseOver', 'doSomething( event )' ); 

// to attach event listeners you have to do a bit more work
// ( best to make a function rather than doing this each time
if( document.addEventListener ) {
    // compliant browsers
    link2.addEventListener('mouseover', doSomething, false);
} else {
    // internet explorer
    link2.attachEvent( 'onmouseover', function() {
        // grab the event object from the window
        var e = window.event;
        // normalise it a bit i.e. make it a bit more like a compliant browsers event object
        e.target = e.srcElement;
        e.preventDefault = function(){ e.returnValue = false };
        e.stopPropagation = function(){ e.cancelBubble = true };
        // and forward to your handler making sure that `this` is properly set
        doSomething.call( this, e );
    });
}

link3.onclick = doSomething;

Note Avoid wrapping your handlers in unecessary anonymous functions, it's wastefull and you lose the this in the handler
so instead of

link3.onclick = function( event ) { doSomething( event ) };

just assign the handler directly

link3.onclick = doSomething;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜