jQuery右鍵菜單contextMenu使用實(shí)例
先上效果圖,是有些同志說(shuō)的有圖才有真相嘛:

ui代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 runat="server">
<title></title>
<%-- --%>
<script src="Script/jquery.js" type="text/javascript"></script>
<script src="Script/jquery.contextmenu.r2.js" type="text/javascript"></script>
<style type="text/css">
.SelectedRow
{
background: yellow;
}
.contextMenu
{
display: none;
}
</style>
<script type="text/javascript">
$(function() {
$('#GridView1 tr:gt(0)').contextMenu('menu',
{
bindings:
{
'add': function(t, target) {
alert('Trigger:' + t.id + ' 增加' + " taget by:" + $("td:eq(0)", target).text());
},
'delete': function(t, target) {
alert('Trigger:' + t.id + ' 刪除' + " taget by:" + $("td:eq(0)", target).text());
$(target).remove();
},
'save': function(t, target) {
alert('Trigger:' + t.id + ' 保存' + " taget by:" + $("td:eq(0)", target).text());
},
'About': function(t, target) {
alert('Code by http://www.cnblogs.com/whitewolf/');
}
},
onShowMenu: function(e, menu) {
if (parseInt($("td:eq(0)", e.currentTarget).text()) > 10) {
$("#save", menu).remove();
}
$(e.currentTarget).siblings().removeClass("SelectedRow").end().addClass("SelectedRow");
return menu;
}
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="contextMenu" id="menu">
<ul>
<li id="add">
<img src="image/Home_Age_UnSelect.jpg" width="16" height="16" />
編輯</li>
<li id="delete">
<img src="image/Home_Age_UnSelect.jpg" width="16" height="16" />
刪除</li>
<li id="save">
<img src="image/Home_Age_UnSelect.jpg" width="16" height="16" />
保存</li>
<li id="About">
<img src="image/Home_Age_UnSelect.jpg" width="16" height="16" />
關(guān)于</li>
</ul>
</div>
<asp:GridView ID="GridView1" runat="server" Width="100%" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3">
<RowStyle ForeColor="#000066" />
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
注:
1:contextMenu我們互根據(jù)數(shù)據(jù)記錄隱藏一些菜單項(xiàng),這個(gè)可以在onShowMenu事件中,根據(jù)
e.currentTarget觸發(fā)源獲取數(shù)據(jù),在根據(jù)remove菜單項(xiàng)。比如測(cè)試用例中的:如果id>10則不允許保存
if (parseInt($("td:eq(0)", e.currentTarget).text()) > 10) {
$("#save", menu).remove();
}
2:事件注冊(cè):根據(jù)第二個(gè)參數(shù)target獲取數(shù)據(jù),第一個(gè)參數(shù)t獲取菜單項(xiàng)。比如:
'add': function(t, target) {
alert('Trigger:' + t.id + ' 增加' + " taget by:" + $("td:eq(0)", target).text());
},
在這里需要用到ajax和服務(wù)端通訊,可以采用我的上一篇組件:jQuery Ajax 仿AjaxPro.Utility.RegisterTypeForAjax輔助方法,將會(huì)更簡(jiǎn)單應(yīng)用ajax通訊。
在我下的源代碼中這里有點(diǎn)問(wèn)題:
原來(lái)的,這里的currentTarget始終為undefined。
if (!!cur.onShowMenu) menu = cur.onShowMenu(e, menu);
$.each(cur.bindings, function(id, func) {
$('#' + id, menu).bind('click', function(e) {
hide();
func(trigger, currentTarget);
});
});
我修改后:
if (!!cur.onShowMenu) menu = cur.onShowMenu(e, menu);
$.each(cur.bindings, function(id, func) {
$('#' + id, menu).bind('click', function(ev) {
hide();
func(trigger, e.currentTarget);
});
});
這樣就一切正常了。
內(nèi)容很少,一切就在此打斷,結(jié)束 ,over!
附件下載:Demo
- jquery事件機(jī)制擴(kuò)展插件 jquery鼠標(biāo)右鍵事件
- JQuery右鍵菜單插件ContextMenu使用指南
- jQuery簡(jiǎn)單實(shí)現(xiàn)禁用右鍵菜單
- jquery 簡(jiǎn)短右鍵菜單 多瀏覽器兼容
- 深入探討JavaScript、JQuery屏蔽網(wǎng)頁(yè)鼠標(biāo)右鍵菜單及禁止選擇復(fù)制
- 基于jQuery的動(dòng)態(tài)增刪改查表格信息,可左鍵/右鍵提示(原創(chuàng)自Zjmainstay)
- jquery實(shí)現(xiàn)在網(wǎng)頁(yè)指定區(qū)域顯示自定義右鍵菜單效果
- jquery禁用右鍵單擊功能屏蔽F5刷新
- jQuery檢測(cè)鼠標(biāo)左鍵和右鍵點(diǎn)擊的方法
- JQuery模擬實(shí)現(xiàn)網(wǎng)頁(yè)中自定義鼠標(biāo)右鍵菜單功能
相關(guān)文章
Jquery實(shí)現(xiàn)彈出層分享微博插件具備動(dòng)畫(huà)效果
此Jquery插件是一款非常實(shí)用的特效,不但有分享功能,還具備了動(dòng)畫(huà)效果,提高了用戶(hù)體驗(yàn),感興趣的朋友可以參考下哈2013-04-04
JQuery自定義事件的應(yīng)用 JQuery最佳實(shí)踐
本文主要介紹JQuery框架里面支持的自定義事件模型,通過(guò)實(shí)例說(shuō)明什么時(shí)候可以利用事件模型進(jìn)行面向?qū)ο蟮腏S編程,以及利用“帶命名空間的事件處理函數(shù)”來(lái)避免unbind時(shí)影響別的事件訂閱。2010-08-08
jQuery學(xué)習(xí)筆記 操作jQuery對(duì)象 CSS處理
在對(duì)class屬性的操作中,實(shí)際上已經(jīng)改變?cè)貥邮搅?,但這些都是根據(jù)已設(shè)定的CSS規(guī)則間接生效的。因此我們有必要使用更為直接的方式來(lái)改變?cè)仍O(shè)定好了的CSS規(guī)則2012-09-09
淺談jQuery中的$.extend方法來(lái)擴(kuò)展JSON對(duì)象
下面小編就為大家?guī)?lái)一篇淺談jQuery中的$.extend方法來(lái)擴(kuò)展JSON對(duì)象。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
jquery捕捉回車(chē)鍵及獲取checkbox值與異步請(qǐng)求的方法
這篇文章主要介紹了jquery捕捉回車(chē)鍵及獲取checkbox值與異步請(qǐng)求的方法,實(shí)例分析了jQuery針對(duì)回車(chē)鍵的捕捉,checkbox值的獲取以及異步請(qǐng)求的響應(yīng)等相關(guān)技巧,需要的朋友可以參考下2015-12-12
jQuery+HTML5+CSS3制作支持響應(yīng)式布局時(shí)間軸插件
這篇文章主要為大家詳細(xì)介紹了JQuery+HTML5+CSS3制作時(shí)間軸,支持響應(yīng)式布局時(shí)間軸插件,感興趣的小伙伴們可以參考一下2016-08-08
jQuery 順便學(xué)習(xí)下CSS選擇器 奇偶匹配nth-child(even)
今天學(xué)習(xí)jQuery,看到nth-child(even)用法,特意找了下這個(gè)選擇器的用法,在CSS3標(biāo)準(zhǔn)中,用法很強(qiáng)大。2010-05-05
jQuery中的insertBefore(),insertAfter(),after(),before()區(qū)別介紹
這篇文章主要介紹了jQuery中的insertBefore(),insertAfter(),after(),before()區(qū)別介紹的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

