问题描述
我的代码中有多个来自不同页面的scriptBlock,但它应该为用CakePHP scriptBlock编写的所有JavaScript创建一个<script>
标签。
我下面的示例代码正在为每个代码创建单独的<script>
标签。
echo $this->Html->scriptBlock(
'jQuery(".investment-table tbody>tr").show()',['block' => true]
);
echo $this->Html->scriptBlock(
'jQuery(".investment-table tbody>tr").hide()',['block' => true]
);
echo $this->Html->scriptBlock(
'jQuery(".investment-table tbody>tr").remove()',['block' => true]
);
解决方法
HtmlHelper::scriptBlock()
默认是为每个调用创建一个<script>
元素。
这应该不是真正的问题,例如:
<script>jQuery(".investment-table tbody>tr").show()</script>
<script>jQuery(".investment-table tbody>tr").hide()</script>
<script>jQuery(".investment-table tbody>tr").remove()</script>
和
<script>
jQuery(".investment-table tbody>tr").show()
jQuery(".investment-table tbody>tr").hide()
jQuery(".investment-table tbody>tr").remove()
</script>
在功能上是相同的。
但是,出于参数的考虑,如果出于某种原因需要将内容放入同一元素,则可以例如写入自定义块,然后在脚本块中输出该自定义块,如下所示其中:
$this->append(
'mergedScripts','jQuery(".investment-table tbody>tr").show();'
);
$this->append(
'mergedScripts','jQuery(".investment-table tbody>tr").remove();'
);
$this->append(
'mergedScripts','jQuery(".investment-table tbody>tr").remove();'
);
echo $this->Html->scriptBlock($this->fetch('mergedScripts'));
另请参见