jquery - 防止jquery多次点击功能

标签 jquery html css

是否有机会调整我的代码,以便当有人单击按钮时代码只会执行一次?不像用户单击该按钮的次数那么多。谢谢
HTML

<button class="click_me">Click</button>
<div class="example">Some text</div>

CSS

.example{
    width:100px;
    padding:5px 5px;
    background:skyblue;
    color:white;
    display:none;
}

JQUERY

$(".click_me").click(function(){
    $(".example").slideDown();
    $(".example").delay("2000").slideUp();
});

另请查看我的 Fiddle !

$(".click_me").click(function() {
  $(".example").slideDown();
  $(".example").delay("2000").slideUp();
});
.example {
  width: 100px;
  padding: 5px 5px;
  background: skyblue;
  color: white;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="click_me">Click</button>
<div class="example">Some text</div>

最佳答案

您可以使用.one() method 。这样做时,该函数仅执行一次。

来自jQuery docs :

The .one() method will attach a handler to an event for the elements. The handler is executed at most once per element per event type.

Updated Example

$(".click_me").one('click', function(){
    $(".example").slideDown();
    $(".example").delay("2000").slideUp();
});

关于jquery - 防止jquery多次点击功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29462634/

相关文章:

javascript - 使用 JavaScript 设置属性时是否需要转义属性?

jquery - 你如何改变相对于绝对的位置?

javascript - 有效地使用for循环

javascript - 执行具有滑动延迟的功能

jquery - 通过 JQuery 添加基于 URL 字符串的 CSS 类

php - 在 wordpress 插件页面中添加可拖动部分

css - Div导航栏背景颜色不显示

html - 对齐 div 以在所有浏览器中工作 >= IE8

css - 为什么这种图像替换技术不起作用?

css - bootstrap : Font-sizes in px, 是否不再使用 em/rem?