jQuery不会在第一次点击时触发,而在第二次点击时触发 [英] jQuery doesn't fire on first click but does on second

查看:72
本文介绍了jQuery不会在第一次点击时触发,而在第二次点击时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$(document).ready(function() {
    $('#talents li').on('click',  'div', function(event) {
        event.stopPropagation();

        $(event.target).addClass('--active');
        talents_points = $('.--active').length;
        _this.setState({ points: talents_points });
        this.handlePointsMaxed(talents_points);
    });
    $('.no-right-click').bind('contextmenu', function(event) {
        event.stopPropagation();

        $(event.target).removeClass('--active');
        talents_points = $('.--active').length;
        _this.setState({ points: talents_points });
        // this.handlePointsMaxed(talents_points);

        return false;
    });
});

<ul id="talents">
    <li onClick={this.handlePointsClicks}>
        <div id="talent-stacks" className="talent-image__stacks"></div>
    </li>
</ul>`

知道为什么吗?我很肯定这是在mousedown和click函数上,但是我需要它们两者来完成我的工作,并且还没有找到将它们组合起来的另一种方法.

编辑:为上下文添加了HTML示例

解决方案

我能够解决此问题,但在react组件的componentDidMount生命周期中没有正确设置功能.<​​/p>

感谢大家的投入.

$(document).ready(function() {
    $('#talents li').on('click',  'div', function(event) {
        event.stopPropagation();

        $(event.target).addClass('--active');
        talents_points = $('.--active').length;
        _this.setState({ points: talents_points });
        this.handlePointsMaxed(talents_points);
    });
    $('.no-right-click').bind('contextmenu', function(event) {
        event.stopPropagation();

        $(event.target).removeClass('--active');
        talents_points = $('.--active').length;
        _this.setState({ points: talents_points });
        // this.handlePointsMaxed(talents_points);

        return false;
    });
});

<ul id="talents">
    <li onClick={this.handlePointsClicks}>
        <div id="talent-stacks" className="talent-image__stacks"></div>
    </li>
</ul>`

Any idea why? I'm positive it's on the mousedown and click functions but I need them both for what I making and have not found another way to combine these.

EDIT: Added the HTML sample for context

解决方案

I was able to fix this, I didn't have the function set correctly in the componentDidMount lifecycle of the react component.

Thanks everyone for your input.

这篇关于jQuery不会在第一次点击时触发,而在第二次点击时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆