当它不是函数中的选择器时更改此元素?

时间:2013-09-07 22:07:57

标签: jquery

我在此尝试实现的目的是将更改应用于breakoff,同时将更改应用于this jQuery('.toggle-bg')

jQuery 我尝试仅对breakoff this

中的.toggle-bg进行更改
    jQuery('.toggle-bg').on('change', function () {

    var value = jQuery('input.switch-id-value',this).val(),
    moon1 = jQuery('#' + value + '_0').is(':checked'),
    breakoff = jQuery('.line-break-off'),
    toggle = jQuery(this);

        toggle.css('background-color', (moon1 ? '#46b692' : '#333'));

        breakoff.each(function() {
            toggle.css('background-color', (moon1 ? '#46b692' : '#333'));
        });

}).trigger('change');

我确信有几件事可以让它发挥作用......例如我需要使用.parent()让我向您展示HTML,以便了解breakoff与...的关系切换。

HTML:

    <li class="top-header">

        <div class="box-title">
            <div class="section-icon fontawesome-twitter">
            <span class="line-decor"></span>
            <span class="line-break-off break"></span> //THE TARGET
            </div>
        </div>

// This is sloppy but this block below contains the toggle you get the idea.. 
// I dont have much experience using .parent() it kinda confuses me
// I am sure I need to nest my way out to target this.parent() .line-break-off

<div class="party">
    <div class="control-block ">
        <fieldset class="buttonset button-switch"><span class="toggle-bg" id="top-header" style="background-color: rgb(51, 51, 51);"><input type="radio" checked="checked" value="1" name="brashup[top-header]" id="top-header_0"><input type="hidden" value="top-header" class="switch-id-value"><input type="radio" value="0" name="brashup[top-header]" id="top-header_1"><input type="hidden" value="top-header" class="switch-id-value"><span class="switch ui-buttonset"><p class="switch-inner-bar"></p></span></span>
        </fieldset>&nbsp;&nbsp;
        <div class="bottom-box"><span class="description btn-desc">Here you can choose if you want a fixed header or not.</span>
        </div>
    </div>
</div>
</li>

简单地说,这是一个包含多个切换的列表,当特定的切换被更改时,我想通过添加一个类来操纵它的父.line-break-off,或者我可以从那里获取css的任何内容:)

1 个答案:

答案 0 :(得分:1)

尝试以下操作。 breakoff应该是这样的:

breakoff = jQuery(this).parents(".top-header").find('.line-break-off')

然后:

breakoff.each(function() {
    jQuery(this).css('background-color', (moon1 ? '#46b692' : '#333'));
});