使用jquery隐藏div

时间:2014-12-16 05:50:33

标签: jquery

我试图隐藏一个div。我成功地隐藏了它。但是,我认为这不是正确的方法。我试图隐藏整个div,内部有很多DOM元素,如span& div ..这是我的代码。我认为我不应该通过使用父母()的次数来显示或隐藏。什么可能是短的或更快的?有什么想法吗?

$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').parent().parent().parent().parent().parent().show();
$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').not(':lt('+(noNcbYear + 1)+')').parent().parent().parent().parent().parent().hide();

我的HTML代码是

                    <div class="col-md-12 ncbclaim-slider raja">

                        <div tooltip-message="If you have claimed before, then select 'Yes'. An NCB (No Claim Bonus) certificate will be required from your previous insurer at the time of claim settlement." showtooltipon="hover" class="claim-details-tooltip" data-original-title="" title="">
                            <div class="touch-icon">
                                <label class="radio checked">
                                    <label for="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes&quot;&quot;">
                                        <div class="radio-box">
                                            <span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span><input type="radio" class="validate basic-required-options" data-toggle="radio"  id="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes" name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim">
                                        </div>
                                        <span class="city-name">0%</span>
                                        <span class="city-name">(1 yrs)</span>
                                    </label>
                                </label>
                            </div>
                        </div>

                        <div title="" data-original-title="" class="claim-details-tooltip" showtooltipon="hover" tooltip-message="If you have claimed before, then select 'Yes'. An NCB (No Claim Bonus) certificate will be required from your previous insurer at the time of claim settlement.">
                            <div class="touch-icon">
                                <label class="radio">
                                    <label for="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes&quot;&quot;">
                                        <div class="radio-box">
                                            <span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span><input type="radio" name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim" id="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes"  data-toggle="radio" class="validate basic-required-options">
                                        </div>
                                        <span class="city-name">20%</span>
                                        <span class="city-name">(2 yrs)</span>
                                    </label>
                                </label>
                            </div>
                        </div>

                        <div title="" data-original-title="" class="claim-details-tooltip" showtooltipon="hover" tooltip-message="If you have claimed before, then select 'Yes'. An NCB (No Claim Bonus) certificate will be required from your previous insurer at the time of claim settlement.">
                            <div class="touch-icon">
                                <label class="radio">
                                    <label for="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes&quot;&quot;">
                                        <div class="radio-box">
                                            <span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span><input type="radio" name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim" id="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes"  data-toggle="radio" class="validate basic-required-options">
                                        </div>
                                        <span class="city-name">25%</span>
                                        <span class="city-name">(3 yrs)</span>
                                    </label>
                                </label>
                            </div>
                        </div>

                        <div title="" data-original-title="" class="claim-details-tooltip" showtooltipon="hover" tooltip-message="If you have claimed before, then select 'Yes'. An NCB (No Claim Bonus) certificate will be required from your previous insurer at the time of claim settlement.">
                            <div class="touch-icon">
                                <label class="radio">
                                    <label for="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes&quot;&quot;">
                                        <div class="radio-box">
                                            <span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span><input type="radio" name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim" id="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes"  data-toggle="radio" class="validate basic-required-options">
                                        </div>
                                        <span class="city-name">35%</span>
                                        <span class="city-name">(4 yrs)</span>
                                    </label>
                                </label>
                            </div>
                        </div>

                        <div title="" data-original-title="" class="claim-details-tooltip" showtooltipon="hover" tooltip-message="If you have claimed before, then select 'Yes'. An NCB (No Claim Bonus) certificate will be required from your previous insurer at the time of claim settlement.">
                            <div class="touch-icon">
                                <label class="radio">
                                    <label for="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes&quot;&quot;">
                                        <div class="radio-box">
                                            <span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span><input type="radio" name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim" id="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes"  data-toggle="radio" class="validate basic-required-options">
                                        </div>
                                        <span class="city-name">45%</span>
                                        <span class="city-name">(5 yrs)</span>
                                    </label>
                                </label>
                            </div>
                        </div>

                        <div tooltip-message="If you have claimed before, then select 'Yes'. An NCB (No Claim Bonus) certificate will be required from your previous insurer at the time of claim settlement." showtooltipon="hover" class="claim-details-tooltip" data-original-title="" title="">
                            <div class="touch-icon">
                                <label class="radio">
                                    <label for="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes&quot;&quot;">
                                        <div class="radio-box">
                                            <span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span><input type="radio" class="validate basic-required-options" data-toggle="radio"  id="eForm_form_details_insurancePolicyToTransfer_noClaimInLastPolicyPeriodYes" name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim">
                                        </div>
                                        <span class="city-name">50%</span>
                                        <span class="city-name">(&gt;5 yrs)</span>
                                    </label>
                                </label>
                            </div>
                        </div>
                    </div>

2 个答案:

答案 0 :(得分:0)

$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').parent().parent().parent().parent().parent().show();
$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').not(':lt('+(noNcbYear + 1)+')').parent().parent().parent().parent().parent().hide();claim-details-tooltip

更改下面的原始代码↓↓↓↓↓

$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').parents('.claim-details-tooltip').show();
$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').not(':lt('+(noNcbYear + 1)+')').parents('.claim-details-tooltip').hide();

下次尝试执行此类操作时,请使用.parents() API。它会转到指定的选择器,因此您不必反复使用.parent()

答案 1 :(得分:0)

试试这个: 您可以使用.parents()选择器。

$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').parents(".claim-details-tooltip").show();
$('.raja input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').not(':lt('+(noNcbYear + 1)+')').parents(".claim-details-tooltip").hide();