duanmibei1929 2014-11-30 21:57
浏览 37
已采纳

混合由noConflict(wordpress)引起的jQuery和php失败

I want to mix jQuery and php in wordpress.

This is my current code:

function theme_output($url) {
echo "<script language='text/javascript'>
    jQuery(function($) {
        var $products,
        $current_product = 'default';

        // List all the products here

        $products = {
    ";
        $themes = array('cerulean', 'cosmo', 'cyborg', 'darkly', 'flatly', 'journal', 'lumen', 'paper', 'readable', 'sandstone', 'simplex', 'slate', 'spacelab', 'superhero', 'united', 'yeti');
        foreach ($themes as $theme) {
            echo 
                $theme." : {
                    name     : '".ucfirst($theme)."',
                    tag      : 'phpBB',
                    img      : '".css_file($theme)."',
                    url      : '".$url."?theme=".$theme."',
                    purchase : 'http://www.google.de',
                },";
        };
echo "
    };
});
    </script>";
};

So there is a "header" in jquery/javascript and then a foreach php part and at least a jquery "footer" part.

The problem is, that the wordpress no conflict (I guess this is the reason), deletes all the $variables.

This is the output: enter image description here

Can somebody help me to fix this?

Thank you

  • 写回答

2条回答 默认 最新

  • dqk42179 2014-12-02 11:30
    关注

    A way to solve the problem is, to put the foreach part into a own function. You should also change the " and the '.

    Like this:

    function theme_output($url) {
    echo '
        <script type="text/javascript">
                var $products,
                $current_product = "default";
    
                // List all the products here
    
                $products = {
    ';
    
    funct2($url);
    
    echo '
                };
        </script>';
    };
    
    function func2($url) {
        $themes = array('cerulean', 'cosmo', 'cyborg', 'darkly', 'flatly', 'journal', 'lumen', 'paper', 'readable', 'sandstone', 'simplex', 'slate', 'spacelab', 'superhero', 'united', 'yeti');
        foreach ($themes as $theme) {
            echo $theme.' : {
                    name     : "'.ucfirst($theme).'",
                    tag      : "phpBB",
                    img      : "'.css_file($theme).'",
                    url      : "'.$url.'?theme='.$theme.'",
                    purchase : "http://www.google.de",
                },
    
            ';
        };
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 51单片机使lcd显示器开机闪烁预设字符闪烁3次需要加什么代码
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题