简体   繁体   中英

window.event doesn't work in firefox

<html>
    <script type="text/javascript">
        function handleMouseEvent(context, element, type){            
            if (!evt) 
                var evt = ((window.event)?(event):(evt));           
            alert(evt);
        }
    </script>
    <body>
        <div id="contain">
            <div id="parent_id" style="background-color: 
                lightblue;position: absolute; 
                left:50px; top:50px; 
                width: 200px; height: 75px;"
                onmousedown="handleMouseEvent(this, 'parent_id', 1)"
                onmouseup="save(this, 1)">
            </div>
        </div>
    </body>
</html>

i have a homework. i must create a rectangle then drag and dop it. but i can catch e.which in firefox. it work ok in chrome and ie9. but doesn't work in firefox

instead of passing parameters, use...

function handleMouseEvent(e)
{
    var evt = e || window.event;
    //here you can get id by using evt.srcElement.id
}

or use this...

function doSomething(e) {
    var rightclick;
    if (!e) var e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
    alert('Rightclick: ' + rightclick); // true or false
}

for your reference... http://www.quirksmode.org/js/events_properties.html

Look this,

https://developer.mozilla.org/en/DOM/window.onmousedown

Firefox itself passes event parameter to the function.

I've modified the MDN function to help you.

<html>
<head>

<title>onmousedown test</title>

<script type="text/javascript">

window.onmousedown = mousedown;

function mousedown(e){ 
 console.log(e.pageX);
 console.log(e.pageY);
 console.log(e.target)
}
</script>
</head>

<body>
<p>click and hold down the LH mouse button<br />
 on the page to fire the mousedown event.</p>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM