js判断右键菜单和大小写键盘是否锁定

How do you tell if caps lock is on using JavaScript?

function isCapslock(e){ e =(e)? e : window.event;var charCode =false;if(e.which){ charCode = e.which;}elseif(e.keyCode){ charCode = e.keyCode;}var shifton =false;if(e.shiftKey){ shifton = e.shiftKey;}elseif(e.modifiers){ shifton =!!(e.modifiers &4);}if(charCode >=97&& charCode <=122&& shifton){returntrue;}if(charCode >=65&& charCode <=90&&!shifton){returntrue;}returnfalse;}

 

It's kind of hard to tell what you're asking, but if you want to hook into the "context menu" event of a browser, you hook the contextmenu event and then do whatever you're going to do (which could include creating a div, for instance, with options on it — e.g., your own context menu). You can either do that on the lists themselves, individually, via getElementById as you indicated in your question, or you can do it by hooking the event on some container that holds all of the lists, and then figuring out when the event is triggered which list it was triggered on ("event delegation").

See the end of this answer for the event delegation approach. But assuming you have a way of knowing the actual IDs used and you want to hook each list specifically for some reason:

HTML:

<ulid='list_1'><li>List 1 item 1</li><li>List 1 item 2</li></ul><ulid='list_2'><li>List 2 item 1</li><li>List 2 item 2</li></ul>

JavaScript:

hookEvent(document.getElementById('list_1'),'contextmenu',function(event){event=event|| window.event;if(event.preventDefault){event.preventDefault();} display("List 1 context menu");returnfalse;}); hookEvent(document.getElementById('list_2'),'contextmenu',function(event){event=event|| window.event;if(event.preventDefault){event.preventDefault();} display("List 2 context menu");returnfalse;});function hookEvent(element,event, handler){if(element.addEventListener){ element.addEventListener(event, handler,false);}elseif(element.attachEvent){ element.attachEvent('on'+event, handler);}else{ element['on'+event]= handler;}}

Live example

Note that only some (most) browsers let you cancel the default context menu.


Update: Re your "but what if the ID is bindable?" question below: I'm afraid I don't know what you mean by "bindable" — none of the tags on your question indicates a specific templating technology. You haven't even mentioned whether the templating is happening server-side or client-side, which makes it hard to help. But basically, by the time the JavaScript is running, there will be real IDs on real elements in the document. You'll have to know what those IDs are in order to use getElementById.

Server-side templating:

If those IDs are going to be completely dynamic and the template is being handled on the server, you can include a small bit of script that passes those IDs on to JavaScript. For instance, near the top of your document you might have:

<scripttype='text/javascript'>var mySpecialListIDs =[];</script>

...and then update your template to add a small script tag each time it's expanded:

<ulid="list_{id}"class="list"><liid="Item_{id}"><aondblclick=""><span>{title}</span></a></li></ul><scripttype='text/javascript'> mySpecialListIDs.push("{id}");</script>

Then your client-side code can loop through mySpecialLitIDs and use each ID when hooking up the handler.

Client-side templating:

If the templating is being done client-side, this gets a bit simpler: Just set up your mySpecialListIDslist at some convenient place in your client-side script, and the append to it each time you call the templating engine.


Event Delegation: Whether you're doing server- or client-side templating, if you're going to have dynamic lists like this, sometimes event delegation is the best way to handle it. The contextmenu event (like most, but not all, events) bubbles up the DOM. So if you hook it on an ancestor element (something that contains all of your lists, like the document body itself or some such), you can then see which actual list was clicked by examining the event object. Like this:

HTML:

<divid='list_container'><ulid='list_1'><li>List 1 item 1</li><li>List 1 item 2</li></ul><ulid='list_2'><li>List 2 item 1</li><li>List 2 item 2</li></ul></div>

JavaScript (using the hookEvent function from above):

// Hook up the contextmenu event on the container, not// on each list: hookEvent(document.getElementById('list_container'),'contextmenu', handleListContextMenu);// Our handler functionfunction handleListContextMenu(event){var target;// Handle IE-vs-the-world differenceevent=event|| window.event;// Find out what the actual target element clicked was target =event.target ||event.srcElement;// See if it or an ancestor of it is one of our listswhile(target &&(target.tagName !=="UL"||!target.id || target.id.substring(0,5)!=="list_")){ target = target.parentNode;}// Did we find a list?if(target){// Yes, handle this.if(event.preventDefault){event.preventDefault();} display("List '"+ target.id +"' context menu");returnfalse;}}

Live example

 

 

 

 

 

 

 

 

你可能感兴趣的:(右键菜单)