需要绕过jquery的toggle功能

发布于 2024-09-06 18:00:30 字数 307 浏览 0 评论 0原文

我需要能够绕过切换的第一个功能,我想知道 jquery 切换是如何工作的?

据我所知,如果我点击某个东西,就会执行第一个功能,然后当我再次点击它时,就会执行第二个功能。

我需要做的是绕过第一个函数并直接进入第二个函数。

我这样做的原因是按钮的状态已被发生的另一个事件更改。现在,当我第一次单击该按钮时,它假定这是第一次单击该按钮,并且当我需要它执行第二个功能时,它将执行第一个功能。

有办法解决这个问题吗?如果 jQuery 切换函数等于 true 或 false,那么我肯定应该能够检查它并调用正确的脚本?

非常感谢!

I need to be able to bypass the first function of a toggle and I was wondering how the jquery toggle works?

I understand that if I click on something, the first function is carried out and then when I click on it again, the second function is carried out.

What I need to be able to do is to bypass the first function and go directly to the second one.

Reason I am doing this is that the state of a button has been changed by another event that's happened. When I now click on that button for the first time, it assumes that it's the first time it's been clicked and will carry out the first function when I need it to carry out the second function.

Is there a way around this? If the jQuery toggle function equates to a true or false, then surely I should be able to check this and call on the correct script?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2024-09-13 18:00:30

我假设另一个事件并不总是在第一次切换之前触发,否则您可以反转这些功能。

您可以更改其他事件来触发切换吗?如果其他事件正在执行相同的任务,那么这将是要走的路。

  // From within the other event's handler
$(someselector).click();  // to fire the toggle

否则,您可以使用 .data() 在元素上放置一个标志来检查 toggle() 是否已触发。如果没有,则进行第一个功能测试以查看该元素是否已被其他事件修改。

$(someselector).toggle(
    function() {
        var $th = $(this);
        if( !$th.data('toggled') ) {  // Check if toggle has not yet run
            $th.data('toggled', true);    // If not, set flag it to show it has run
            if( some_test_to_see_if_modified_externally ) {
                $th.click() // fire the second toggle
                return;     // return if element has been modified externally
            }
        } 
        // otherwise run the normal code  
    },
    function() {
        // your normal code
    }
);

I assume that the other event doesn't always fire before the first toggle, otherwise you could just reverse the functions.

Could you change your other event to fire the toggle? If the other event is performing the same task, then this would be the way to go.

  // From within the other event's handler
$(someselector).click();  // to fire the toggle

Otherwise, you could use .data() to place a flag on the element to check if the toggle() has fired yet. If it hasn't, then have the first function test to see if the element has been modified by the other event.

$(someselector).toggle(
    function() {
        var $th = $(this);
        if( !$th.data('toggled') ) {  // Check if toggle has not yet run
            $th.data('toggled', true);    // If not, set flag it to show it has run
            if( some_test_to_see_if_modified_externally ) {
                $th.click() // fire the second toggle
                return;     // return if element has been modified externally
            }
        } 
        // otherwise run the normal code  
    },
    function() {
        // your normal code
    }
);
(り薆情海 2024-09-13 18:00:30

您可以在切换函数之外创建一个变量:var isFirstToggle = true;

然后在您的切换回调中,如果等于 true,则返回;

var isFirstToggle = true;
$("#myId").toggle(function(){
   if (isFirstToggle === true){
      isFirstToggle = false;
      return;
}, function(){
   //do stuff on second toggle.
});

让我知道您的想法以及这是否合适。可能有更干净的方法来处理它,但这应该可以快速完成。

You could create a variable outside of the toggle function: var isFirstToggle = true;.

Then in your toggle callback just return if it is equal to true;

var isFirstToggle = true;
$("#myId").toggle(function(){
   if (isFirstToggle === true){
      isFirstToggle = false;
      return;
}, function(){
   //do stuff on second toggle.
});

Let me know your thoughts and if this seems appropriate. There is probably cleaner ways to handle it, but this should work quick-n-dirty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文