简体   繁体   English

Jquery 按钮触发整个页面上的任何点击事件

[英]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.隐藏文本类称为 compleatelist。

However, the button triggers when I click on ANY click event in the whole page not my ".more" button?但是,当我点击整个页面中的任何点击事件而不是我的“.more”按钮时,按钮会触发?

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.这只会在单击包含more类的元素时触发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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