如何将Javascript对象的所有属性放入另一个对象?

时间:2016-06-08 16:18:26

标签: javascript jquery twitter-bootstrap twitter-bootstrap-wizard

我正在使用波尔图的定制JQuery Bootstrap向导,我被要求了 当使用链接时,它将进入向导的相关选项卡。

我唯一需要的是从以下对象变量中检索所有属性:

$('#w3').bootstrapWizard({
    tabClass: 'wizard-steps',
    nextSelector: 'ul.pager li.next',
    previousSelector: 'ul.pager li.previous',
    previousLinkSelector: 'ul.pager li.finish a.previous-link',
    firstSelector: null,
    lastSelector: null,
    onNext: function (tab, navigation, index, newindex) {
        var validated = $('#w3 form').valid();
        if (!validated) {
            $w3validator.focusInvalid();
            return false;
        }
    },
    onTabClick: function (tab, navigation, index, newindex) {
        if (newindex == index + 1) {
            return this.onNext(tab, navigation, index, newindex);
        } else if (newindex > index + 1) {
            return false;
        } else {
            return true;
        }
    },
    onTabChange: function (tab, navigation, index, newindex) {
        var $total = navigation.find('li').size() - 1;
        $w3finish[newindex != $total ? 'addClass' : 'removeClass']('hidden');
        $('#w3').find(this.nextSelector)[newindex == $total ? 'addClass' : 'removeClass']('hidden');
    },
    onTabShow: function (tab, navigation, index) {
        var $total = navigation.find('li').length - 1;
        var $current = index;
        var $percent = Math.floor(($current / $total) * 100);
        $('#w3').find('.progress-indicator').css({ 'width': $percent + '%' });
        tab.prevAll().addClass('completed');
        tab.nextAll().removeClass('completed');
    }
});

我需要将它放在一个全局变量中,以便我可以使用所有属性 (尤其是onTabClick属性)。

我尝试过以下方式:

var $mainSettings = $.fn.bootstrapWizard;

问题是$ mainSettings只包含函数声明。

从$ .fn.bootstrapWizard获取所有属性的正确方法是什么? ?

1 个答案:

答案 0 :(得分:0)

这取决于此jquery插件如何公开它接收的配置。如果它没有暴露这个,那么你运气不好。但是,您可以将配置分配给变量并将该变量传递给jquery插件。

var $mainSettings = {
    tabClass: 'wizard-steps',
    nextSelector: 'ul.pager li.next',
    previousSelector: 'ul.pager li.previous',
    previousLinkSelector: 'ul.pager li.finish a.previous-link',
    firstSelector: null,
    lastSelector: null,
    onNext: function (tab, navigation, index, newindex) {
        var validated = $('#w3 form').valid();
        if (!validated) {
            $w3validator.focusInvalid();
            return false;
        }
    },
    onTabClick: function (tab, navigation, index, newindex) {
        if (newindex == index + 1) {
            return this.onNext(tab, navigation, index, newindex);
        } else if (newindex > index + 1) {
            return false;
        } else {
            return true;
        }
    },
    onTabChange: function (tab, navigation, index, newindex) {
        var $total = navigation.find('li').size() - 1;
        $w3finish[newindex != $total ? 'addClass' : 'removeClass']('hidden');
        $('#w3').find(this.nextSelector)[newindex == $total ? 'addClass' : 'removeClass']('hidden');
    },
    onTabShow: function (tab, navigation, index) {
        var $total = navigation.find('li').length - 1;
        var $current = index;
        var $percent = Math.floor(($current / $total) * 100);
        $('#w3').find('.progress-indicator').css({ 'width': $percent + '%' });
        tab.prevAll().addClass('completed');
        tab.nextAll().removeClass('completed');
    }
};

$('#w3').bootstrapWizard($mainSettings);