1,本插件兼容以下浏览器:IE,Firefox,Chrome,Safari,TT
2,js代码部分
<script> /** * jquery右键菜单 * edit by www.jb200.com */ // eg: $("#id").jsRightMenu({ // menuList:[{ menuName: "右键菜单", clickEvent: "divClick('click1')"}] // }); jQuery.fn.extend({ jsRightMenu: function(options) { options = $.extend({ menuList: [] }, options); return this.each(function() { if ($("#div_RightMenu", $(this)).size() == 0) { var menuCount = options.menuList.length; if (menuCount > 0) { var divMenuList = "<div id="div_RightMenu" class="div_RightMenu">"; for (var i = 0; i < menuCount; i++) { divMenuList += "<div class="divMenuItem" onclick="" + options.menuList[i].clickEvent + "" onmouseover="" + options.menuList[i].mouseoverEvent + "" onmouseout="" + options.menuList[i].mouseoutEvent + "">" + options.menuList[i].menuName + "</div>"; } divMenuList += "</div>"; $(this).append(divMenuList); var objM = $(".divMenuItem"); $("#div_RightMenu").hide(); objM.bind("mouseover", function() { this.style.backgroundColor = "#316ac5"; this.style.paddingLeft = "30px"; }); objM.bind("mouseout", function() { this.style.backgroundColor = '#EAEAEA'; }); } } this.oncontextmenu = function() { var objMenu = $("#div_RightMenu"); if (objMenu.size() > 0) { objMenu.hide(); var event = arguments[0] || window.event; var clientX = event.clientX; var clientY = event.clientY; var redge = document.body.clientWidth - clientX; var bedge = document.body.clientHeight - clientY; var menu = objMenu.get(0); var menuLeft = 0; var menuTop = 0; if (redge < menu.offsetWidth) menuLeft = document.body.scrollLeft + clientX - menu.offsetWidth; else menuLeft = document.body.scrollLeft + clientX; if (bedge < menu.offsetHeight) menuTop = document.body.scrollTop + clientY - menu.offsetHeight; else menuTop = document.body.scrollTop + clientY; objMenu.css({ top: menuTop + "px", left: menuLeft + "px" }); objMenu.show(); return false; } } document.onclick = function() { var objMenu = $("#div_RightMenu"); if (objMenu.size() > 0) objMenu.hide(); } }); } }); </script>
3,html部分
<div id="divTest">右键菜单显示区域</div>
4,调用实例
首先,下载最新版本jquery,最好去Juqery官网上下。
然后在页面的<head></head>之间加入下面代码:
<script src="../js/jquery-1.7.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function() { $("#divTest" ).jsRightMenu({ menuList: [{ menuName: "右键菜单1", clickEvent: "divClick('1')"}, { menuName: "右键菜单2", clickEvent: "divClick('2')"}, { menuName: "右键菜单3", clickEvent: "divClick('3')"}, { menuName: "右键菜单4", clickEvent: "divClick('4')"}, { menuName: "右键菜单5", clickEvent: "divClick('5')"} ] }); }); function divClick(id){ alert("点击了--右键菜单"+id); } </script>