admin管理员组

文章数量:1399251

I want to add a class to the <body> tag to make the cursor a crosshair when clicking a button. When I click any where on the page, I then want this class to be removed.

Currently I click the button, the class is added, and then removed straight away.

$('button').on('click', function() {
  $('body').addClass('changeCursor');
}

$('html').on('click', '.changeCursor', function(){
  $('body.changeCursor').removeClass('changeCursor');
});

I want to add a class to the <body> tag to make the cursor a crosshair when clicking a button. When I click any where on the page, I then want this class to be removed.

Currently I click the button, the class is added, and then removed straight away.

$('button').on('click', function() {
  $('body').addClass('changeCursor');
}

$('html').on('click', '.changeCursor', function(){
  $('body.changeCursor').removeClass('changeCursor');
});
Share Improve this question edited Feb 29, 2016 at 16:26 Mr Lister 46.6k15 gold badges113 silver badges155 bronze badges asked Feb 25, 2016 at 11:55 jonnowjonnow 8582 gold badges11 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

It's removed straight-away because the button click propagates to the html element after your button click handler adds the class to body. Then the html element's handler runs, finds body.changeCursor, and removes it.

Just stop propagation in the first handler:

$('button').on('click', function(e) {
  $('body').addClass('changeCursor');
  e.stopPropagation();
});

Live Example:

$('button').on('click', function(e) {
  $('body').addClass('changeCursor');
  e.stopPropagation();
});

$('html').on('click', '.changeCursor', function() {
  $('body.changeCursor').removeClass('changeCursor');
});
.foo {
  display: none;
}
body.changeCursor .foo {
  display: inline-block;
}
<button type="button">Click me to add</button>
<p>Click here or anywhere but the button above to remove</p>
<p class="foo">I'm only showing when <code>body</code> has <code>changeCursor</code></p>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

There I've used stopPropgation which only stops propagation, but if you don't need the default action to occur, you can just return false, which does both stopPropgation and preventDefault:

$('button').on('click', function() {
  $('body').addClass('changeCursor');
  return false;
});

Live Example:

$('button').on('click', function() {
  $('body').addClass('changeCursor');
  return false;
});

$('html').on('click', '.changeCursor', function() {
  $('body.changeCursor').removeClass('changeCursor');
});
.foo {
  display: none;
}
body.changeCursor .foo {
  display: inline-block;
}
<button type="button">Click me to add</button>
<p>Click here or anywhere but the button above to remove</p>
<p class="foo">I'm only showing when <code>body</code> has <code>changeCursor</code></p>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

本文标签: