简体   繁体   English

尝试将点击事件附加到只会触发一次的ID

[英]trying to attach a click event to an id that only fires once

UPDATE turns out the code is actually working see my answer below UPDATE证明代码实际上正在工作,请参阅下面的答案

I'm having some troubles here. 我在这里遇到一些麻烦。 I thought I found my answer in the .one method, but apparently, .one means ONLY ONCE PER PAGE PER ANYTHING EVER which isn't exactly what I was going for. 我以为我在.one方法中找到了答案,但是显然,.one意味着每页仅一次,任何一次,这都不是我想要的。 Here's what my intention was: 这是我的意图:

$("#someID").one('mouseover', function() {

//do some stuff

});

$("#someOtherID").one('mouseover', function() {

//do some stuff

});

My expectation was that once that first one fired, that mouseover event would no longer fire for THAT ELEMENT. 我的期望是,一旦第一个事件触发,该鼠标悬停事件将不再为那个元素触发。

The problem with this is that once the first one fires, the second one will not fire either. 问题在于,一旦第一个触发,第二个也不会触发。 So the .one method appears to be disabling ALL mouseover events for ALL elements after that first one fires. 因此,.one方法似乎在第一个触发后为所有元素禁用了所有鼠标悬停事件。

I did not expect this, I expected the .one to only apply to that first element. 我没想到这一点,我希望.one只适用于第一个元素。 Is this just a flaw in my understanding of the .one method or am I coding wrong? 这只是我对.one方法的理解上的缺陷还是我编码错误?

If it's just a flaw in my understanding, could someone point me in the right direction to correct my code? 如果这只是我的理解上的缺陷,那么有人可以指出正确的方向来纠正我的代码吗?

Thank you in advance! 先感谢您!

This is embarassing, I hope I don't get dinged for this and blocked again from stackoverflow (the easiest thing ever to get blocked from and the hardest to get unblocked). 这真是令人尴尬,我希望我不会为此而感到厌烦并再次阻止其堆栈溢出(有史以来最容易被阻止并且最难被阻止的事情)。

First, @CertainPerformance, thanks for taking the time to look at my question. 首先,@ CertainPerformance,感谢您抽出宝贵时间来查看我的问题。 My real code didn't have the two mistakes you mentioned, I updated my post to reflect the correct syntax. 我的实际代码没有您提到的两个错误,我更新了我的文章以反映正确的语法。

I'll be honest, my code is working now, and I have no idea why. 老实说,我的代码正在运行,我也不知道为什么。 I suspect I've been dealing with some crazy caching issues which frustrates me because I'm using inMotionHosting which has really great reviews, and I have caching disabled in cPanel. 我怀疑我一直在处理一些疯狂的缓存问题,这些问题使我感到沮丧,因为我使用的inMotionHosting的评论非常好,并且在cPanel中禁用了缓存。

If anything, maybe this thread will benefit somebody searching "how to make event fire only once in javascript". 如果有的话,也许这个线程会有益于搜索“如何使事件仅在javascript中触发一次”的人。

You could make the callback run once like this: 您可以像这样使回调运行一次:

    // Extend the function prototype
    Function.prototype.once = function() {

        // Variables
        var func = this, // Current function
            result;

        // Returns the function
        return function() {

            // If function is set
            if(func) {

                // Executes the function
                result = func.apply(this, arguments);

                // Unset the function, so it will not be called again
                func = null;

            }

            // (:
            return result;

        };

    };

    // Bind the event to the function you will use as a callback
    $("#someID").on('mouseover', function() {

       console.log('just once');

    }.once());

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

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