在附加js文件时使用headscript()添加属性attributes
Add attribute attributes while appending a js file using headscript()
这是我第一次使用php。我试图优化js在一个网站,我看到headscript()
附加js文件。
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js');
在这个我试图添加一个属性的脚本标签附加
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
我也试过
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',$attrs = array("async" => "true"))
array("async" => "true");
文件php部分
<?php
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $attrs = array("async" => "true"));
?>
我期望输出是
<script async="true" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
我得到
<script type="Array" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
如何解决?我找不到任何通过headscript();
您需要将这些属性作为第三个参数传递
$this->headScript()->appendFile(
'/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
null,
array('async' => 'true', 'foo' => 'bar')
);
null
这里是'type'属性,默认为text/javascript
看起来你正在使用Zend
框架。如果有,请参考HeadScript Helper
您需要传递属性为$attrs = array()
appendFile($src, $type = 'text/javascript', $attrs = array())
所以你的代码应该是
<<p> 更新代码/strong> echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $type = 'text/javascript', $attrs = array("async" => "true"));
代替
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', array("async" => "true"));
默认禁用任意属性。要允许这样的属性,您可以通过setAllowArbitraryAttributes()方法启用它们:
$this->headScript()->setAllowArbitraryAttributes(true);
然后echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', null, array("async" => "true"));
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 如何将歌曲添加到jPlayer
- 在html Select中添加搜索
- Java脚本时间添加
- FabricJs-限制主对象内添加对象的移动区域
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 如何在Google柱状图中动态添加行/列
- 正在尝试使用if和else添加类,但无法正常工作
- 在附加js文件时使用headscript()添加属性attributes