Javascript Jquery子div选择

Javascript Jquery子div选择,javascript,php,jquery,Javascript,Php,Jquery,我使用switchy,它使用animate-color.js 我有不止一个,不同于他们的页面,每次一个得到toogle,所有的页面都变成绿色,我如何防止这样一个只得到toogle $(function () { $('.binary').switchy(); $('.binary').on('change', function () { // Animate Switchy Bar background color 7cb15b var bgCol

我使用switchy,它使用animate-color.js

我有不止一个,不同于他们的页面,每次一个得到toogle,所有的页面都变成绿色,我如何防止这样一个只得到toogle

$(function () {
    $('.binary').switchy();
    $('.binary').on('change', function () {
        // Animate Switchy Bar background color 7cb15b
        var bgColor = '#ebebeb';
        if ($(this).val() == '1') {
            bgColor = '#7cb15b';
        } else if ($(this).val() == '0;') {
            bgColor = '#ebebeb';
        }
        $('.switchy-bar').animate({
            backgroundColor: bgColor
        });
        // Display action in console
        var log = 'Selected value is "' + $(this).val() + '"';
        $('#console').html(log).hide().fadeIn();
    });
});
你可以在www.niors.com上看到我的意思

$('.switchy-bar').animate({
    backgroundColor: bgColor
});

我们的想法是只需更改一个与您正在单击或选择的元素相关的切换栏…

此处:

$('.switchy-bar').animate({
    backgroundColor: bgColor
});
您可以从文档中获得所有的
切换条。所以你需要找到你需要的背景,也就是这样:

$(this).parent().find('.switchy-bar').animate({
    backgroundColor: bgColor
});
#(此)
->选择

$(this).parent()
->包装器(
.field

然后在切换栏中搜索已更改的选择。 当然,为了获得更好的性能和可读性,最好在回调函数的开头将
$(this)
缓存到某个变量中

大概是这样的:

$(function () {
    $('.binary').switchy();
    $('.binary').on('change', function () {
        var $select = $(this);
        var selectedValue = $select.val();

        // Animate Switchy Bar background color 7cb15b
        var bgColor = {
            '0': '#ebebeb',
            '1': '#7cb15b'
        };

        $select.parent().find('.switchy-bar').animate({
            backgroundColor: bgColor[ selectedValue ]
        });

        // Display action in console
        var log = 'Selected value is "' + selectedValue + '"';
        $('#console').html(log).hide().fadeIn();
    });
});

.switchy bar
元素与
.binary
元素的关系如何?你能展示一下你喜欢分享的HTMLany HTML标记吗?@user2726228我们为什么要在那里查看它?如果你不能为了你自己的利益而分享它…很抱歉,我在电话里问这个问题时,没能得到它
$(function () {
    $('.binary').switchy();
    $('.binary').on('change', function () {
        var $select = $(this);
        var selectedValue = $select.val();

        // Animate Switchy Bar background color 7cb15b
        var bgColor = {
            '0': '#ebebeb',
            '1': '#7cb15b'
        };

        $select.parent().find('.switchy-bar').animate({
            backgroundColor: bgColor[ selectedValue ]
        });

        // Display action in console
        var log = 'Selected value is "' + selectedValue + '"';
        $('#console').html(log).hide().fadeIn();
    });
});