Javascript實(shí)現(xiàn)的簡(jiǎn)單右鍵菜單類
本文實(shí)例講述了Javascript實(shí)現(xiàn)的簡(jiǎn)單右鍵菜單類。分享給大家供大家參考。具體如下:
這是自己寫的一個(gè)右鍵菜單類,屏蔽掉了IE固有的右鍵菜單,一共有四個(gè)參數(shù):第一個(gè)是出發(fā)顯示右鍵菜單的div的id
第二個(gè)是右鍵菜單這個(gè)層的id,根據(jù)這個(gè)id去創(chuàng)建一個(gè)新的層,menuList是菜單項(xiàng)的列表,對(duì)應(yīng)了點(diǎn)擊一個(gè)菜單項(xiàng)后觸發(fā)的函數(shù),classList是菜單的class名稱,以及菜單項(xiàng)對(duì)應(yīng)的class名稱,包含了鼠標(biāo)滑過(guò)時(shí)的class。
運(yùn)行效果截圖如下:
在線演示地址如下:
http://demo.jb51.net/js/2015/js-right-button-menu-class-codes/
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>右鍵菜單</title> <style type="text/css"> .cmenu { position:absolute; top:100px; left:100px; width:200px; height:200px; background-color:white; border:1px solid pink; } .liAble { font-family:"宋體"; color:#6699CC; margin-left:10px; margin-top:5px; list-style-type:none; cursor:default; } .liMouseOver { margin-left:10px; margin-top:5px; background-color:#CCFFFF; list-style-type:none; cursor:default; } </style> </head> <body> <div style="margin-left:auto; margin-right:auto; height:300px; width:60%;background-color:#CC6699" id="x"> </div> <input type="hidden" id="value1" value="4" /> <input type="hidden" id="value2" value="5" /> <script type="text/javascript"> //右鍵菜單類 function RightHandMenu(div,menuDiv,menuList,classList) { var oThis = this; this._menuList = { } this._classList = { objClass:'', MenuClass:'', liAbleClass:'', liMouseOverClass:'' } this.Init = function() { this._obj = $(div); this._obj.oncontextmenu = function(e){oThis.ShowMenu(e)}; this._obj.className = this._classList.objClass; document.onclick = function(){oThis.HiddenMenu()}; objToObj(this._classList, classList); objToObj(this._menuList, menuList); } this.CreateMenu = function() { if($(menuDiv)) { alert("該ID已被占用"); return; } this._menu = document.createElement("DIV"); this._menu.id = menuDiv; this._menu.oncontextmenu = function(e){stopBubble(e)}; this._menu.className = this._classList.MenuClass; this._menu.style.display = "none"; document.body.appendChild(this._menu); } this.CreateMenuList = function() { for(var pro in this._menuList) { var li = document.createElement("LI"); li.innerHTML = pro; this._menu.appendChild(li); li.className = this._classList.liAbleClass; li.onclick = this._menuList[pro]; li.onmouseover = function(){oThis.ChangeLiClass(this,oThis._classList.liMouseOverClass)} li.onmouseout = function(){oThis.ChangeLiClass(this,oThis._classList.liAbleClass)} } } this.ChangeLiClass = function(obj,name) { obj.className = name } this.ShowMenu = function(e) { var e = e || window.event; stopBubble(e); var offsetX = e.clientX; var offsetY = e.clientY; with(this._menu.style) { display = "block"; top = offsetY + "px"; left = offsetX + "px"; } } this.HiddenMenu = function() { this._menu.style.display = "none"; } this.Init(); this.CreateMenu(); this.CreateMenuList(); } function stopBubble(oEvent) { if(oEvent.stopPropagation) oEvent.stopPropagation(); else oEvent.cancelBubble = true; if(oEvent.preventDefault) oEvent.preventDefault(); else oEvent.returnValue = false; } function $(div) { return 'string' == typeof div ? document.getElementById(div) : div; } function objToObj(destination,source) { for(var pro in source) { destination[pro] = source[pro]; } return destination; } //構(gòu)造右鍵菜單 function Edit() { alert("edit"); } function Delete() { alert("delete"); } var menuList = { 編輯:Edit, 刪除:Delete } var classList = { MenuClass:'cmenu', liAbleClass:'liAble', liMouseOverClass:'liMouseOver' } var x = new RightHandMenu("x","testDiv",menuList,classList) </script> </body> </html>
希望本文所述對(duì)大家的JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JavaScript 獲取任一float型小數(shù)點(diǎn)后兩位的小數(shù)
這篇文章主要介紹了JavaScript如何獲取小數(shù)任一小數(shù)點(diǎn)后的位數(shù)的小數(shù),需要的朋友可以參考下2014-06-06JS實(shí)現(xiàn)slide文字框縮放伸展效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)slide文字框縮放伸展效果代碼,涉及JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11基于JS實(shí)現(xiàn)經(jīng)典的井字棋游戲
井字棋作為我們?cè)谏蠈W(xué)時(shí)代必玩的一款連珠游戲,承載了很多人的童年記憶。本文我們就用HTML、css、js來(lái)實(shí)現(xiàn)一款井字棋游戲,感興趣的可以動(dòng)手嘗試一下2022-04-04基于JavaScript+HTML5 實(shí)現(xiàn)打地鼠小游戲邏輯流程圖文詳解(附完整代碼)
打地鼠小游戲大家都喜歡玩,本文是以html編寫的,并且使用HBulider軟件進(jìn)行編寫的,下面通過(guò)本文給大家分享基于JavaScript+HTML5 實(shí)現(xiàn)打地鼠小游戲邏輯流程圖文詳解,需要的朋友參考下吧2017-11-11使用Visual?Studio?2022開(kāi)發(fā)前端的詳細(xì)教程
這篇文章主要介紹了使用Visual?Studio?2022開(kāi)發(fā)前端,在瀏覽器中可以使用CDN源引用需要的客戶端庫(kù),可如果在企業(yè)內(nèi)網(wǎng)使用,無(wú)法訪問(wèn)CDN源時(shí),需要將客戶端庫(kù)下載到本地使用,需要的朋友可以參考下2022-05-05JavaScript判斷數(shù)組的方法總結(jié)與推薦
這篇文章主要給大家介紹了關(guān)于JavaScript判斷數(shù)組方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02layui.js實(shí)現(xiàn)的表單驗(yàn)證功能示例
這篇文章主要介紹了layui.js實(shí)現(xiàn)的表單驗(yàn)證功能,結(jié)合實(shí)例形式分析了基于layui.js的事件監(jiān)聽(tīng)、驗(yàn)證、判定等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11