php - CakePHP 2 $this->Html->脚本顺序

标签 php cakephp cakephp-2.1

我正在尝试将 JS 文件插入到 View 中,但它们的插入顺序错误。

在我的 default.ctp 中有这个

$this->Html->script(array(
    'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
    'global'
), array('inline'=>false));

echo $this->fetch('script');

在我看来我有这个:

$this->Html->script('jquery.fancybox.pack', array('inline' => false));

但是当我查看源代码时,它是这样的:

<script type="text/javascript" src="/js/jquery.fancybox.pack.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="/js/global.js">

这显然是错误的顺序,因此 jQuery 插件无法正常工作。

我做错了什么?

最佳答案

通常,我在布局中回显所需的脚本(而不是将它们添加到缓冲区),然后是脚本 block (缓冲脚本)。这确保首先回显每个 View 所需的脚本。您的 default.ctp 看起来像这样:

// get echoed immediately
echo $this->Html->script(array(
    'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
    'global'
));
// everything else from the view, echoed after
echo $this->fetch('script');

或者,您可以为前面的脚本指定一个特殊 block 。

echo $this->Html->script(array(
    'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
    'global'
), array('block' => 'firstScripts');
echo $this->fetch('css');
echo $this->fetch('firstScripts');
echo $this->fetch('script');

关于php - CakePHP 2 $this->Html->脚本顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904372/

相关文章:

jquery - 如何在cakePHP中使用jquery

database - CakePHP 2.1 按自定义顺序显示记录

cakephp-2.1 - 将两个相关模型的字段组合成显示字段蛋糕 PHP 2.0

cakephp - 如何在CakePHP 2.0的 Controller 中导入类?

php - 存储在 sql 查询中获取的整个元素

php - 以一种形式更改密码和更改电子邮件地址

php - cakephp - 按分页中的二级关联排序

php - 在moodle File API中永久保存后删除草稿文件

php - 如何在运行时生成或修改 PHP 类?

javascript - 将 javascript 与 cakephp 连接起来