这是从自定义控件传递数据的正确方法吗。js自定义预览。js?
( function( api ) {
api.controlConstructor[\'typography\'] = api.Control.extend( {
ready: function() {
var control = this;
function addGoogleFont(fontName) {
var font = control.params.ogf_fonts[fontName];
var weights = $.map(font.variants, function(value, key) {
return key;
});
var weightsURL = weights.join(\',\');
var fontURL = font.family.replace(\' \',\'+\') + \':\' + weightsURL;
wp.customize.previewer.send( \'olympusFontURL\', "<link href=\'https://fonts.googleapis.com/css?family=" + fontURL + "\' rel=\'stylesheet\' type=\'text/css\'>" );
}
control.container.on( \'change\', \'.typography-font-family select\',
function() {
var value = jQuery( this ).val();
control.settings[\'family\'].set( value );
if( value != \'default\' ) {
addGoogleFont( value );
var font = control.params.ogf_fonts[value];
var weightsSelect = jQuery( \'.typography-font-weight select\' );
var newWeights = font.variants;
weightsSelect.empty();
$.each( newWeights, function( key, val ) {
weightsSelect.append( $( "<option></option>" )
.attr( "value", key ).text( val ) );
});
}
}
);
}
} );
} )( wp.customize );
这比试图.append()
直接在自定义控件中预览iframe。js?这里是自定义预览。js适用于感兴趣的任何人:
jQuery( document ).ready( function() {
wp.customize.bind( \'preview-ready\', function() {
wp.customize.preview.bind( \'olympusFontURL\', function( url ) {
console.log(url);
$("head").append(url);
} );
} );
} ); // jQuery( document ).ready