jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 在可能多次调用的事件上只调用一次特定函数?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个实时点击功能,需要始终执行certian事件,但有一件事必须只在第一次点击时执行,之后,需要禁用它.

$('.theImage').live("click",function(){ 
            // The commands within the if statement must only happen on the first click.
    if (!chkHeightOnceOnly){
        var containerHeight = $(this).children(".posTinfo").height();
        $(this).children(".posTinfo").height(containerHeight - 2);
        var chkHeightOnceOnly = true; 
    }
            // other commands which need to fire on every click go here
});

因此,目前每次点击div,它都会减去2px.它只需要第一次减去2px.

编辑 – 这是针对.theImage的许多实例,它们是通过ajax进入的,因此需要.live()

解决方法

我会将标记存储在HTML标记本身中.

$('.theImage').live("click",function(){ 
    // The commands within the if statement must only happen on the first click.
    if ($(this).attr('data-once')!='already' ){
        var containerHeight = $(this).children(".posTinfo").height();
        $(this).children(".posTinfo").height(containerHeight - 2);

        $(this).attr('data-once','already');
    }
    // other commands which need to fire on every click go here
});

大佬总结

以上是大佬教程为你收集整理的jquery – 在可能多次调用的事件上只调用一次特定函数?全部内容,希望文章能够帮你解决jquery – 在可能多次调用的事件上只调用一次特定函数?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。