Javascript 使用headscript()在附加js文件时添加属性

Javascript 使用headscript()在附加js文件时添加属性,javascript,php,zend-framework2,Javascript,Php,Zend Framework2,这是我第一次使用php。我在一个网站上尝试优化js,我看到headscript()附加js文件 echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js'); 在本文中,我试图添加脚本标记附加的属性 $this->headScript()->setAllowArbitraryAttributes(true); echo $this->headScr

这是我第一次使用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"))
数组(“异步”=>“真”)

文件中的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()添加属性的示例

看起来您正在使用
Zend
框架。如有,请参阅


您需要将属性传递为
$attrs=array()

所以你的代码应该是

更新代码

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"));

您需要将属性作为第三个参数传递

$this->headScript()->appendFile(
  '/js/image-gallery/jquery.simplemodal.1.4.4.min.js', 
  null, 
  array('async' => 'true', 'foo' => 'bar')
); 

null
这里是“type”属性,默认为
text/javascript

默认情况下禁用任意属性。要允许这些属性,可以通过setAllowArbitraryAttributes()方法启用它们:

然后


如果您联系php脚本的作者以找到正确的语法,那就更好了。您提供的信息不足,无法使用。@jeff:我找不到任何信息。我在这里发布了这个问题,希望任何尝试过这个的人都能帮助我。仍然会发生同样的事情。你能粘贴
$this->headScript()->appendFile()
的代码吗?你添加了
函数调用代码
,但是
函数定义代码
是必需的,因为这似乎不是一个标准的API。你能帮我一下吗。这是我第一次使用
php
,我无法找到使用
headscript
的完整代码。-1您发布了正确的方法签名,但您的示例都尝试使用
$type
参数来包含属性。属性数组应作为第三个参数传递。
$this->headScript()->appendFile(
  '/js/image-gallery/jquery.simplemodal.1.4.4.min.js', 
  null, 
  array('async' => 'true', 'foo' => 'bar')
); 
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',  null, array("async" => "true"));