简体   繁体   English

jquery遍历父级以更改css

[英]jquery traversing to parent to change css

I have a multiple drop down lists on a page: 我在页面上有多个下拉列表:

HTML HTML

<div class="select-full">
    <select id="activitylevel" class="input_select step2-ddl c2-sb-enabled">
        <option selected="selected" value="-1">-- please select --</option>
        <option value="1">Step 1 </option>
        <option value="3">Step 2</option>
        <option value="4">Step 3</option>
    </select>
    <div class="c2-sb-wrap" tabindex="0">
        <div class="c2-sb-inner-wrap"></div>
    </div>
</div>

I want to apply a border to the div with class c2-sb-inner-wrap to any selects that have not been selected (value == -1) 我想使用类c2-sb-inner-wrap将div应用于任何尚未选择的选项(value == -1)

I'm trying to use closest() and next/nextAll but it's not happening: 我正在尝试使用nearest()和next / nextAll但它没有发生:

$('.step2-ddl').each(function() {

    if ($(this).val() == '-1') {
        $(this).closest('.select-full').nextAll('.c2-sb-inner-wrap').css('border-color', '#f86556').css('border-style', 'solid');
    }
    else {
        $(this).closest('.select-full').nextAll('.c2-sb-inner-wrap').css('border-style', 'none');
    }
});​

Use .find() instead of .nextAll() 使用.find()代替.nextAll()

Like: 喜欢:

$(this).closest('.select-full').find('.c2-sb-inner-wrap')
       .css('border-color', '#f86556').css('border-style', 'solid');

As a side note, I'd suggest toggling classes rather than directly changing CSS properties, as it's both leaner and cleaner. 作为旁注,我建议切换类而不是直接更改CSS属性,因为它既更精简又更清晰。

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

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