WordPress在有内置的 wp_script_add_data 与 wp_style_add_data 实现注册有条件的 javascript 和 css。可以通过这个来做IE浏览器兼容的识别。
IE 9一下增加 html5 shiv
// Load the html5 shiv. wp_enqueue_script( 'html5', 'https://unpkg.com/[email protected]/dist/html5shiv.min.js', array(), null ); wp_script_add_data( 'html5', 'conditional', 'lt IE 9' );
解析后:
<!--[if lt IE 9]> <script type='text/javascript' src='https://unpkg.com/[email protected]/dist/html5shiv.min.js'></script> <![endif]-->
IE 8与IE 8以下增加css
// Load the Pure.css base. wp_enqueue_style( 'base', 'https://unpkg.com/[email protected]/build/base-min.css',array(),null); wp_style_add_data( 'base', 'conditional', 'lte IE 8' );
解析后:
<!--[if lte IE 8]> <link rel="stylesheet" id="base" href="https://unpkg.com/[email protected]/build/base-min.css"> <![endif]-->
参考资料:
https://developer.wordpress.org/reference/functions/wp_script_add_data/
https://developer.wordpress.org/reference/functions/wp_style_add_data/