jQuery .on('点击',function()仅适用于第二次点击

时间:2016-04-11 23:21:49

标签: javascript jquery wordpress onclick woocommerce

我有一个自定义的jQuery脚本,其中包含.click();函数工作正常,但.hide();和.show();只触发第二次点击。

HTML:

<div id="ivpa-content" class="ivpa-register ivpa_registered" data-variations="TLDR">    
    <div class="ivpa_attribute ivpa_text ivpa_background ivpa_selection" data-attribute="pa_foetel">
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="elso-foetel"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="masodik-foetel"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="foetel-nelkul"></span>
    </div>
    <div class="ivpa_attribute ivpa_text ivpa_background ivpa_selection" data-attribute="pa_foetel-adag">
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="foetel-fel-adag"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="foetel-teljes-adag"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="a-foetel-nelkul"></span>
    </div>
    <div class="ivpa_attribute ivpa_text ivpa_background ivpa_selection" data-attribute="pa_leves">
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="elso-leves"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="masodik-leves"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="leves-nelkul"></span>
    </div>
    <div class="ivpa_attribute ivpa_text ivpa_background ivpa_selection" data-attribute="pa_leves-adag">
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="leves-fel-adag"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="leves-teljes-adag"></span>
        <span class="ivpa_term ivpa_active ivpa_instock" data-term="a-leves-nelkul"></span>
    </div>
    <a class="ivpa_reset_variations" href="#reset">Clear selection</a>
</div>

jQuery的:

(function ($) {
var foetel_nelkul = $('[data-term="foetel-nelkul"]');
var foetel_adag = $('[data-attribute="pa_foetel-adag"]');
var elso_foetel = $('[data-term="elso-foetel"]');
var a_foetel_nelkul = $('[data-term="a-foetel-nelkul"]');
var foetel_fel_adag = $('[data-term="foetel-fel-adag"]');
var foetel_teljes_adag = $('[data-term="foetel-teljes-adag"]');

$(a_foetel_nelkul).hide();
$(foetel_adag).hide();

$(elso_foetel).on('click', function () {
    $(foetel_adag).show();
    if ($(foetel_nelkul).hasClass('ivpa_clicked')) {
        foetel_nelkul.click();
    }
    if ($(a_foetel_nelkul).hasClass('ivpa_clicked')) {
        a_foetel_nelkul.click();
    }
});
$(foetel_nelkul).on('click', function () {
    $(foetel_adag).hide();
    if ($(elso_foetel).hasClass('ivpa_clicked')) {
        elso_foetel.click();
        if ($(foetel_fel_adag).hasClass('ivpa_clicked')) {
            foetel_fel_adag.click();
        }
        if ($(foetel_teljes_adag).hasClass('ivpa_clicked')) {
            foetel_teljes_adag.click();
        }
        if ($(a_foetel_nelkul).hasClass('ivpa_clicked')) {
            a_foetel_nelkul.click();
        }
    }

所以当我点击它显示的元素$(elso_foetel)时,我需要在$(foetel_nelkul)上点击两次以隐藏$(foetel_adag);反之亦然。

提前感谢所有帮助!我真的需要通过这个。

1 个答案:

答案 0 :(得分:0)

也许您可以将功能从.on()更改为:

$(foetel_nelkul).click(function() {
    // All of your code in the other function here
});