我正在尝试在自定义程序中设置条件显示项目的多个实例。此代码适用于conditional\\u display()函数的任何一个实例,但不会同时执行这三个实例
(function() {
function conditional_display(conditional, condition_toggled) {
wp.customize.control(conditional, function(control) {
//conditions are the value(s) for controller that will trigger toggling
conditions = condition_toggled.split(\',\');
toggleControl = function toggleControl(value) {
conditions.forEach(function(condition) {
//this will get the condition that will result in display
//and controllers of displayed items
cond_display_control = condition.split(\'|\');
//separates out the controllers to be displayed
controllers = cond_display_control[1].split(\'^\');
if (value == cond_display_control[0]) {
controllers.forEach(function(controller) {
wp.customize.control(controller).toggle(true);
});
} else {
controllers.forEach(function(controller) {
wp.customize.control(controller).toggle(false);
});
}
});
};
toggleControl(control.setting.get());
control.setting.bind(toggleControl);
});
}
/**
* Run functions when customizer is ready.
*/
wp.customize.bind(\'ready\', function() {
conditional_display(\'jma_gbs_site_border_control\', \'yes|jma_gbs_site_border_color_control^jma_gbs_site_border_width_control^jma_gbs_site_border_radius_control\');
conditional_display(\'jma_gbs_modular_header_control\', \'yes|jma_gbs_header_border_color_control^jma_gbs_header_border_width_control^jma_gbs_header_border_radius_control\');
conditional_display(\'jma_gbs_modular_footer_control\', \'yes|jma_gbs_footer_border_color_control^jma_gbs_footer_border_width_control^jma_gbs_footer_border_radius_control\');
});
})();
有什么想法吗?
最合适的回答,由SO网友:Tim Elsass 整理而成
是-看起来您打算创建的变量只是在每次调用中重新分配。您应该使用var
每次之前:
var conditions, toggleControl; // here.
conditions = condition_toggled.split(\',\');
toggleControl = function toggleControl(value) {
conditions.forEach(function(condition) {
var cond_display_control, controllers; // here.
//this will get the condition that will result in display
//and controllers of displayed items
cond_display_control = condition.split(\'|\');
//separates out the controllers to be displayed
controllers = cond_display_control[1].split(\'^\');
if (value == cond_display_control[0]) {
controllers.forEach(function(controller) {
wp.customize.control(controller).toggle(true);
});
} else {
controllers.forEach(function(controller) {
wp.customize.control(controller).toggle(false);
});
}
});
};