简体   繁体   English

如何绑定点击触发器,仅点击元素?

[英]how to bind click trigger, only to clicked element?

I have some elements having fbutton class. 我有一些有fbutton类的元素。 How can I bind a click element only to clicked element. 如何仅将click元素绑定到单击的元素。 I have a code like this: 我有这样的代码:

    $('.fbutton').click(function() {

        /*
            Some Code 1
        */
            $(this).next().click();
        /*
            Some Code 2
        */
            enough;
    });

$(this).next().click(); line triggers a click on an element, but also all other fbutton elements are triggered too ( this part bothers me ). line触发了对元素的单击,但也触发了所有其他fbutton元素( 这部分困扰我 )。

First solution that comes to mind is that end script processing after Some Code 2 . 首先想到的解决方案是在一些代码2之后的最终脚本处理。 As return; 作为回归; does not work, I used an illegal javascript code that results abnormal end of execution of code. 不起作用,我使用非法的JavaScript代码导致代码执行异常结束。 It works , but it is not the correct way of ending execution. 它有效 ,但它不是结束执行的正确方法。 How can I end javascript execution or How can I trigger an event only for clicked element. 如何结束javascript执行或如何仅针对单击的元素触发事件。


More Clarification 更多澄清
I want only clicked .fbutton to trigger. 我只想点击.fbutton来触发。 So does jquery identify clicked element? 那么jquery是否识别出被点击的元素? Also I can not define any class for any .fbutton element, because trigger should depend on user click. 此外,我无法为任何.fbutton元素定义任何类,因为触发器应取决于用户单击。

Have you tried using stopPropagation? 你尝试过使用stopPropagation吗?

$('.fbutton').on('click',function(e){
    e.stopPropagation();
   $(this).next().trigger('click'); 
});

onclick give it a class, and bind the click triggering to it. onclick给它一个类,并将单击触发绑定到它。

$('.fbutton').on('click',function() {

    // Code 1

    $(this).hasClass('clicked')) ? $(this).next().trigger('click') : $(this).addClass('clicked');

    // Code 2

});

Also, using jQuery vs $? 另外,使用jQuery vs $? I kept it consistent for you, but if you can use $ I recommend it. 我保持一致,但如果你可以使用$我推荐它。

EDIT 编辑

To clarify my answer: 澄清我的答案:

This is a ternary if statement. 这是一个三元if声明。 It checks to see if the item clicked has a class of clicked , and if it does, then it assigns the click you want to $(this).next() , but if it doesn't have the class already it adds the class (without binding the click statement yet). 它检查是否点击的项目有一个类的clicked ,如果确实如此,那么它分配单击要$(this).next()但如果它不具有类已经把它添加类(尚未绑定click语句)。 When the element is clicked again, it will have class clicked , so it should fire then. 当再次单击该元素时,它将clicked该类,因此它应该触发。

This allows you to only have the click event binded to elements that have already been clicked. 这允许您仅将click事件绑定到已单击的元素。

SECOND EDIT 第二次编辑

Here is a jsFiddle to show it works . 这是一个显示它有效的jsFiddle

Click example once and nothing will happen but adding the class, click it again and it will trigger the click of the next button, but not anything else. 单击示例一次,没有任何事情会发生,但添加类,再次单击它将触发单击下一个按钮,但不会发生任何其他操作。 The same is true for the second button, so that you can see they are separate events for each pairing, unrelated to any other button pairs. 对于第二个按钮也是如此,因此您可以看到它们是每个配对的单独事件,与任何其他按钮对无关。

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

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