简体   繁体   English

使用动态添加的类在元素上单击无法识别

[英]Click not recognized on element with dynamically added class

I have the following javascript object: 我有以下javascript对象:

TeamExpand = {
    trigger: '.team-info h2.initial',
    container: '.team-info',
    closeTrigger: '.team-info h2.expanded',
    init: function() {
       jQuery(this.trigger).click(this.expandInfo.bind(this));
       jQuery(this.closeTrigger).click(this.closeInfo.bind(this));
    },
    expandInfo: function(e) {
        jQuery(e.currentTarget).closest('.team-info').css("height", "100%");
        jQuery(e.currentTarget).removeClass("initial");
        jQuery(e.currentTarget).addClass("expanded");
        jQuery(this.socialSVG).attr("fill", "#ffc40c");
    },
    closeInfo: function(e) {
        jQuery(e.currentTarget).closest('.team-info').css("height", "64px");
        jQuery(e.currentTarget).removeClass("expanded");
        jQuery(e.currentTarget).addClass("initial");
        jQuery(this.socialSVG).attr("fill", "white");
    }
}

My html is as follow: 我的HTML如下:

<div class="team-info">
    <h2 class="initial">Header</h2>
    <h3>Job Title</h3>
    <p>Bio</p>
</div><!--end team-info-->

The 'expandInfo' function is running just fine and changed the 'container' height to 100%; 'expandInfo'函数运行正常,并将'容器'高度更改为100%; The 'initial' class is removed from the h2 and the 'expanded' class is added to the h2 . h2删除'initial'类,并将'extended'类添加到h2 But the click event on the 'closeTrigger' variable (the h2.expanded) element is not registering. 但是'closeTrigger'变量(h2.expanded)元素上的click事件没有注册。 What am I doing wrong? 我究竟做错了什么?

I'd rewrite this a bit to make the event handlers simpler. 我重写了一下,使事件处理程序更简单。 Use one handler for all the h2 and just check the class. 对所有h2使用一个处理程序,然后检查类。 This way you avoid attaching detaching handlers. 这样可以避免连接拆卸处理程序。

TeamExpand = {
    trigger: '.team-info h2',
    container: '.team-info',
    init: function() {
       jQuery(this.trigger).click(this.doTriger.bind(this));
    },
    doTriger: function(e) {
        var element = jQuery(e.currentTarget);
        if (element.hasClass('initial')) {
            this.expandInfo(element);
        } else {
            this.closeInfo(element);
        }
    },
    expandInfo: function(element) {
        element.closest('.team-info').css("height", "100%");
        element.removeClass("initial");
        element.addClass("expanded");
        jQuery(this.socialSVG).attr("fill", "#ffc40c");
    },
    closeInfo: function(element) {
        element.closest('.team-info').css("height", "64px");
        element.removeClass("expanded");
        element.addClass("initial");
        jQuery(this.socialSVG).attr("fill", "white");
    }
}

I think it's because you're applying the click event function to an element that doesn't actually exist (the h2 element doesn't yet have the .expanded class). 我认为这是因为你将click事件函数应用于一个实际上不存在的元素( h2元素还没有.expanded类)。

Try moving this line of code.. 尝试移动这行代码..

jQuery(this.closeTrigger).click(this.closeInfo.bind(this));

..to the end of your expandInfo function, and add this.. ..到你的expandInfo函数的末尾,并添加这个..

jQuery(this.closeTrigger).unbind('click');

..to your closeInfo function before this line.. ..到你之前的closeInfo函数..

jQuery(e.currentTarget).removeClass("expanded");

Hope this helps! 希望这可以帮助!

Full code.. 完整代码..

TeamExpand = {
    trigger: '.team-info h2.initial',
    container: '.team-info',
    closeTrigger: '.team-info h2.expanded',
    init: function() {
       jQuery(this.trigger).click(this.expandInfo.bind(this));
    },
    expandInfo: function(e) {
        jQuery(e.currentTarget).closest('.team-info').css("height", "100%");
        jQuery(e.currentTarget).removeClass("initial");
        jQuery(this.trigger).unbind('click');
        jQuery(e.currentTarget).addClass("expanded");
        jQuery(this.socialSVG).attr("fill", "#ffc40c");
        jQuery(this.closeTrigger).click(this.closeInfo.bind(this));
    },
    closeInfo: function(e) {
        jQuery(e.currentTarget).closest('.team-info').css("height", "64px");
        jQuery(this.closeTrigger).unbind('click');
        jQuery(e.currentTarget).removeClass("expanded");
        jQuery(e.currentTarget).addClass("initial");
        jQuery(this.socialSVG).attr("fill", "white");
        this.init();
    }
}

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

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