简体   繁体   中英

Jquery button triggers on any click event on whole page

This is probably a very simple question.

here's my code.

$( document.body ).click(function () {
  if ( $( ".more" ).first().is( ":hidden" ) ) {
    $( ".compleatelist" ).slideDown( 6000 );
  } else {
    $( ".compleatelist" ).hide();
  }
});
  • I'm trying to create a "read more" button that slides down to reveal more info.
  • The button class is called more.
  • the hidden text class is called compleatelist.

However, the button triggers when I click on ANY click event in the whole page not my ".more" button?

What am I missing here?

$( document.body ).click() <-- This means you are firing click on all clicks on the document body

You probably want

$(".more").click(function(e){
    // $(this)  <-- this is the element you just clicked on
});

This will only fire an event when an element containing the more class is clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM