【发布时间】:2016-01-25 09:21:50
【问题描述】:
我正在运行此脚本以显示不同的移动菜单:
<script>
jQuery(document).ready(function($){
if ($(window).width() > 750){
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'container' => false, 'menu_class' => 'menu clearfix' ) ); ?>
}else{
<?php wp_nav_menu( array( 'theme_location' => 'mobile', 'container' => false, 'menu_class' => 'menu clearfix' ) ); ?>
}
});
</script>
但我得到错误的 chrome "Uncaught SyntaxError: Unexpected token
编辑:嗯,不知道在js里面运行php有问题。 那么我的情况的正确解决方案是什么?我知道我可以只使用 css 媒体查询和显示属性,但这对性能不利,不是吗?
【问题讨论】:
-
这是因为您的 PHP 代码在 JS
script块内生成 HTML - 因此出现语法错误。我不太确定你想要达到什么目的。 -
你想用php代码实现什么?
-
我正在尝试显示在
register_nav_menu()注册的菜单 -
如果您使用媒体查询,对性能的影响将是微不足道的,因为导航将设置为 display:none
标签: javascript jquery wordpress google-chrome