如何仅定位具有特定文本的元素?

时间:2014-05-20 15:07:36

标签: javascript jquery

我试图仅针对LI" Refundable"和#34;无需存款"。部分地,我理解为什么其他LI也是目标。但是,如何将列表仅隔离到包含"最佳灵活率"?

的父级
<div class="regularRates roomsView">
    <div class="rateTypeLineItem "> <span>Advance Purchase</span>

        <ul>
            <li>Non-Refundable</li>
            <li>Deposit required</li>
            <li class="BREAKFAST_TWO_GUESTS">Breakfast included</li>
            <li>Must purchase 14 days in advance</li>
        </ul>
    </div>
    <!-- /end rateTypeLineItem -->
</div>
<!-- /end regularRates/roomsView -->
<div class="regularRates roomsView">
    <div class="rateTypeLineItem "> <span class="groupHeader">Best Flexible Rate</span>

        <ul>
            <li>Refundable</li>
            <li>No deposit required</li>
            <li class="BREAKFAST_TWO_GUESTS">Breakfast included</li>
            <li>Must purchase 14 days in advance</li>
        </ul>
    </div>
    <!-- /end rateTypeLineItem -->
</div>
<!-- /end regularRates/roomsView -->
<div class="regularRates roomsView">
    <div class="rateTypeLineItem "> <span>Staybridge Weekends</span>

        <ul>
            <li>Non-Refundable</li>
            <li>Deposit required</li>
            <li class="BREAKFAST_TWO_GUESTS">Breakfast included</li>
            <li>Must purchase 14 days in advance</li>
        </ul>
    </div>
    <!-- /end rateTypeLineItem -->
</div>
<!-- /end regularRates/roomsView -->

这是我脚本的一部分:

//add color and weight to each best Flex Rate with  LI containing Refundable
jQuery(':contains("Best Flexible Rate") ul li span.refundable').each(function () {
    if (jQuery(this).text().indexOf('Refundable') > -1) {
        jQuery(this).attr('style', '');
        jQuery(this).css({
            'color': '#E37C00',
                'font-weight': 'bold'
        });
    }
});
//add color and weight to each best Flex Rate with  LI containing No Deposit
jQuery(':contains("Best Flexible Rate") ul li span.noDeposit').each(function () {
    if (jQuery(this).text().indexOf('No deposit required') > -1) {
        jQuery(this).attr('style', '');
        jQuery(this).css({
            'color': '#E37C00',
                'font-weight': 'bold'
        });
    }
});

您可以在此处查看其余内容:http://jsfiddle.net/bkmills1/5kjLF/2/

感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:0)

如果您想定位包含此文字的

  • 标记,请务必在查询中说明,否则父母也会成为目标:

    jQuery('li:contains("Best Flexible Rate") ul li span.noDeposit')
    

    不确定这是否是您的意思

      

    但是如何将列表仅隔离到包含“Best”的父级   弹性汇率“?

  • 答案 1 :(得分:0)

    你可以改变这一行:

    if (jQuery(this).text().indexOf('Refundable') > -1)
    

    到此:

    if (jQuery(this).text().indexOf('Refundable') === 0)
    

    这样就无法匹配&#34;不可退款&#34;同样。