【JS】HTML5事件

1、contextmenu(windows的右键菜单的意思)

<html>
<head>
    <title>ContextMenu Event Example</title>
    <script type="text/javascript" src="EventUtil.js"></script>
</head>
<body>
    <div id="myDiv">Right click or Ctrl+click me to get a custom context menu. Click anywhere else to get the default context menu.</div>
    <ul id="myMenu" style="position:absolute;visibility:hidden;background-color:silver">
        <li><a href="http://www.nczonline.net">Nicholas' site</a></li>
        <li><a href="http://www.wrox.com">Wrox site</a></li>
        <li><a href="http://www.yahoo.com">Yahoo!</a></li>
    </ul>
    <script type="text/javascript">
        EventUtil.addHandler(window, "load", function(event){
            var div = document.getElementById("myDiv");
                        
            EventUtil.addHandler(div, "contextmenu", function(event){
                event = EventUtil.getEvent(event);
                EventUtil.preventDefault(event);
                
                var menu = document.getElementById("myMenu");
                menu.style.left = event.clientX + "px";
                menu.style.top = event.clientY + "px";
                menu.style.visibility = "visible";
            });
            //设置click事件让菜单隐藏
            EventUtil.addHandler(document, "click", function(event){
                document.getElementById("myMenu").style.visibility = "hidden";
            });
        });

    </script>
</body>
</html>

 

2、window的beforeunload事件

EventUtil.addHandler(window, "beforeunload", function(event){
    event = EventUtil.getEvent(event);
    var message = "I'm really going to miss you if you go.";
    //针对IE和fiefox
    event.returnValue = message;
    //针对safari和Chrome
    return message;
});

 

posted @ 2013-05-20 16:03  自行车上的程序员  阅读(777)  评论(0编辑  收藏  举报