Home > Web Front-end > JS Tutorial > jquery custom right-click menu, select all, discontinuous selection_jquery

jquery custom right-click menu, select all, discontinuous selection_jquery

WBOY
Release: 2016-05-16 15:12:42
Original
1240 people have browsed it

Recently, I have to implement some custom effects in the project, such as right-click menu, select all, discontinuous selection, etc. I think the main thing is to clarify the logic and event relationships. To achieve this, there are also ready-made plug-ins available, such as jQuery UI's selectable.

1. Right-click menu
When browsing the web, right-click the mouse (or ctrl+left-click on the touchpad) and the browser's default right-click menu item will appear, like this:

But when you want to customize the right click on an element, like this:

You must first disable the browser's default menu and listen to the contextmenu event. The key code is as follows:

$(function(){
 $('#box').on('contextmenu',function(event){
 event.preventDefault();
  $(this).trigger('click');
  $('#menulist').css({
   top: event.pageY,
   left: event.pageX
  });
 });
 $('#box').click(function(){
  $('#menulist').css('display','block');
 });
})

Copy after login

2. Select all
The default ctrl+A (command+A under MAC) will select the entire web page or a focused editable element.

<div id='box'>
 <p class='first'>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
</div>
<div id='other'>
 <p class='first'>这是另外一个div,这是另外一个div,</p>
 <p>这是另外一个div,这是另外一个div,</p>
 <p>这是另外一个div,这是另外一个div,</p>
 <p>这是另外一个div,这是另外一个div,</p>
</div>
Copy after login

If there are only these two divs on the page, press ctrl/cmd+A and both divs will be selected. If you only want to select the content of div#box, the simple way is to add contentEditable=true to the div. Another way is to listen for keyboard events.

Simulate selecting all sub-elements p of div#box and highlighting them:

$(function(){
 $(document).keydown(function(event){
 //windows下是event.ctrlKey
 if(event.metaKey && event.which === 65){
  event.preventDefault();
  $('#box>p').trigger('click');
 }
 });
 $('#box').on('click', 'p', function(){
 $(this).css('color','red');
 });
});

Copy after login

3. Shift enables continuous selection
Shift is combined with the right mouse button to achieve continuous selection of elements, which is simply simulated here.

<div id='box' class="unselect">
 <p class='first'>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
 <p>这是个div,这是个div,</p>
</div>
Copy after login

When holding shift, the browser has a default continuous selection, disable it first:

.unselect{
 -webkit-user-select: none;
 -moz-user-select: none;
 -ms-user-select: none;
 -o-user-select: none;
 user-select: none;
}
Copy after login

For lower versions of IE, use the selectstart event:

$('#box')[0].onselectstart = function(e){
  e.preventDefault();
  return false;
 };
Copy after login

Register the click event for p, and listen to the keydown and keyup events of the document object:

$(document).keydown(function(e){
  if(e.shiftKey){
   shiftkey = 1;
  }
 }).keyup(function(){
  shiftkey = 0;
 });
$('#box').on('click','p',function(){
  if(shiftkey === 1){
   second = $(this).index();
   for(var min = Math.min(first,second); min <= Math.max(first,second); min++){
    $('#box').find('p').eq(min).css('color','red');
   }
  } else {
   first = $(this).index();
   $(this).css('color','red').siblings().css('color','black');
  }
 })

Copy after login

4. Discontinuous selection
Discontinuous selection requires monitoring the ctrl key (command key under mac) and registering a click event for the element.

 $(document).keydown(function(e){
  if(e.metaKey){ //win是e.ctrlKey
   ctrlkey = 2;
  }
 }).keyup(function(){
  ctrlkey = 0;
 });
 $('#box').on('click','p',function(){
  if(ctrlkey === 2){
   $(this).css('color','red');
  } else {
   $(this).css('color','red').siblings().css('color','black');
  }
 })
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone in learning jquery programming.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template