jquery(移动):使用翻转开关来改变可折叠内容的主题

时间:2012-04-04 14:01:43

标签: jquery mobile jquery-mobile

我有一堆可折叠的套装,每个都是一个翻转开关,用于发布数据。 结果(有效)是

-itemA YES
-itemB NO
-itemC YES
...
-itemZ NO

我想有一个视觉线索,选择一个集合(开关隐藏在集合中,所以当关闭时,我不知道它是否被选中)

我发现我可以使用主题来更改外观,但是如何更改仅作为交换机父级的可折叠集的主题。

1 个答案:

答案 0 :(得分:3)

您可以更改可折叠小部件的主题特定类以“突出显示”它,这是一个示例:

//setup the classes and theme letters to use for on/off states
var classes = {
        on  : 'ui-btn-up-e ui-body-e',
        off : 'ui-btn-up-c ui-body-c'
    },
    themes  = {
        on  : 'e',
        off : 'c'
    };

//delegate the event handler binding for all toggle switches
$(document).on('change', '.ui-slider-switch', function () {

    //if the switch is "off"
    if ($(this).val() == 'off') {

        //find the parent collapsible widget of this switch, change its theme,
        //find the descendant header link and change it's theme attribute as well as class,
        //then go back to the collapsible selection and find the content wrapper
        //and change it's class to the "off" state class
        $(this).closest('.ui-collapsible').attr('data-theme', themes.off).find('a').attr('data-theme', themes.off).removeClass(classes.on).addClass(classes.off)
               .end().find('.ui-collapsible-content').removeClass(classes.on).addClass(classes.off);
    } else {

        //this does the same but backwards to make the "on" or active state
        $(this).closest('.ui-collapsible').attr('data-theme', themes.on).find('a').attr('data-theme', themes.on).removeClass(classes.off).addClass(classes.on)
               .end().find('.ui-collapsible-content').removeClass(classes.off).addClass(classes.on);
    }
});​

演示:http://jsfiddle.net/ZtJyL/

一些文档:

请注意,我创建的classes对象存储了on的两个类和off的两个类,当使用{{1}时,这两个类将在元素中添加/删除对象。我没有看到在我的JSFiddle中执行此操作有任何冲突,但请注意,这是一个不必要的快捷方式。