简体   繁体   English

我们可以将jQuery one()用于具有多个元素的属性类吗

[英]can we use the jquery one() for attribute class having multiple elements

What I am trying to do is, I have different DIV element having different ID 's and same class 我想做的是,我有不同的DIV元素,具有不同的ID和相同的class

What I know is: 我所知道的是:

 $(id).one() 

and

 $(class).one() 

it work as given in the doc here : Attach a handler to an event for the elements. 它的工作方式如doc 此处所示 :将处理程序附加到元素的事件。 The handler is executed at most once per element per event type. 每个事件类型的每个元素最多只能执行一次处理程序。

What I want: 我想要的是:

 $(class).one()

The handler is executed at most once per ID ie once per DIV element per event type. 该处理程序每​​个ID最多执行一次,即每种事件类型的每个DIV元素执行一次。 and not only once for a class? 不仅一次上课?

Solution I know: 我知道的解决方案:

$(id1).one()

$(id2).one()

$(id3).one() ...

write it as many time as ID's I have which I don't want 写下I don't want ID的次数

is it possible? 可能吗?

Thanks in advance! 提前致谢!

If I understand you correctly, that you want the click to trigger once per class, the one function with selector parameter should do the trick: 如果我正确理解您的要求,即希望每个类触发一次单击,则具有selector参数的one函数应该可以解决问题:

$(document).one("click", ".div-group", function(){
    alert(this.id);
});

Demo here 在这里演示

not sure what are you asking for . 不知道你在问什么。 but most once per element per event type class selector should work most once per element per event type类选择器most once per element per event type只能工作一次

 <div class="click">click1</div>
<div class="click">click2</div>

 $('.click').one("click",function(){
       alert('clicked');
 })

fiddle here 在这里摆弄

Im guessing you want to add a one() to all element of that class, but each of those items may only be clicked once: 我想您想向该类的所有元素添加一个one() ,但是这些项中的每一项只能被单击一次:

<div class="click">click1</div>
<div class="click">click2</div>

$('.click').one("click",function(){
    $(this).off('click'); // torn off click event
    alert( 'This element will not alert this message again' );
})

暂无
暂无

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

相关问题 使用JQuery,我们如何使用一个函数将多个按钮的click事件用于显示对话框 - Using JQuery, how can we use one function to use for click event of multiple buttons to display a dialog box 如何使用jQuery来更改组中除一个元素之外的所有元素的属性 - How to use jQuery to change attribute of all elements in a group except one 使用jquery获取多个元素的属性值。 然后用它们 - Using jquery to get attribute value for multiple elements. Then use them jQuery如何为具有相同类名的多个元素激活一个事件 - jQuery how to activate one event for multiple elements with same class name 使用 Jquery 选择多个元素,但使用 addClass() 仅向其中一个添加一个类 - Selecting multiple elements with Jquery, but adding a class only to one of them with addClass() 在同一个类的多个元素上运行一个jQuery函数 - Run one jQuery function on multiple elements with the same class 我们可以使用和运算符来组合多个jQuery事件 - can we use and operator to combine multiple jQuery events 我们可以使用一个Java脚本函数对多个输入进行验证吗? - Can we use one java script function to multiple input validation? 使用jQuery我们如何从值为false的所有元素中删除disabled属性? - Using jQuery how can we remove disabled attribute from all elements whose value is false? 我们可以使用 jquery 删除 div 类中具有 id 的 div - Can we remove the div having the id inside the div class using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM