由于某些原因,js和css文件没有加载到我的插件中。以下是我所拥有的:(表面上不相关的代码替换为“…”)
class Location_Specific_Menu_Items {
private static $instance = null;
public static function get_instance() {
return null == self::$instance ? self::$instance = new self : self::$instance;
}
function __construct() {
if( is_admin() ) {
...
add_action( \'admin_enqueue_scripts\', array( &$this, \'xsmi_load_admin_script\' ) );
} else {
...
}
function xsmi_load_admin_script() {
wp_enqueue_style(\'chosen.min\', \'https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css\');
wp_enqueue_script(\'jquery\', \'http://code.jquery.com/jquery-1.8.3.js\');
wp_enqueue_script(\'chosen.jquery.min\', \'https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.js\', array(), \'1.0.0\', true);
} // I tried locally hosted files as well.
...
function option( $fields, $item_id ) {
ob_start(); ?>
<p class="field-visibility description description-wide">
<label for="edit-menu-item-visibility-<?php echo $item_id; ?>">
<?php echo \'LSMI Visibility Control\' ?>
<script type="text/javascript">
jQuery(document).ready(function($){
$(".chzn-select").chosen();
});
</script>
<br/><br/>
<select name="chzn-select" class="chzn-select" multiple="true" width="200px">
<option value="AT">Austria</option>
<option value="AU">Australia</option>
</select>
<br/><br/>
...
但看起来是这样的:
如果我发表评论
jQuery(document).ready(function($){
$(".chzn-select").chosen();
});
看起来是这样的:
知道我做错了什么吗?
编辑:
好的,css和js现在正在从CDN加载。然而,该字段的宽度仍然是0px!
最合适的回答,由SO网友:j8d 整理而成
这修复了它。。。
function xsmi_load_admin_script() {
wp_register_style( \'chosencss\', plugins_url( \'assets/chosen.css\', __FILE__ ), true, \'\', \'all\' );
wp_register_script( \'chosenjs\', plugins_url( \'assets/chosen.jquery.js\', __FILE__ ), array( \'jquery\' ), \'\', true );
wp_enqueue_style( \'chosencss\' );
wp_enqueue_script( \'chosenjs\' );
}