您需要等待创建小部件控件,然后才能尝试。您可以通过收听add
事件wp.customize.control
. 下面是一些示例代码,可以实现您想要的功能:
( function( api ) {
function handleTextWidgetControl( control ) {
if ( ! control.extended( api.Widgets.WidgetControl ) ) {
return;
}
if ( \'text\' !== control.params.widget_id_base ) {
return;
}
/*
* Make sure the widget\'s contents are embedded; normally this is done
* when the control is expanded, for DOM performance reasons.
*/
control.embedWidgetContent();
// Now we know for sure the widget is fully embedded.
console.info( control.container.find( \'textarea\' ).attr( \'id\' ) );
}
api.control.each( handleTextWidgetControl );
api.control.bind( \'add\', handleTextWidgetControl );
})( wp.customize );