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条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码