CakePHP 元素 - 如何放置 javascript?

发布于 2024-11-30 22:21:11 字数 199 浏览 2 评论 0原文

我刚刚开始使用 cakephp 元素,它们非常棒(我曾经使用 include)。

我有一个元素库和一个名为注释的元素(针对某个页面),对于它们,我在它们两个上附加了一些 javascript 代码。您对如何在元素中包含该 javascript 代码有什么建议吗?如果我只是简单地添加它,它会在元素后面加载 html 之前加载 javascript,我认为这不是很明智。

I've just started using cakephp elements and they're awesome (I used to use include).

I have an element gallery and an element called comments (for a certain page) and for them I have some javascript code attached to both of them. Do you have any suggestions on how I could include that javascript code in the element? If I simply add it like that it will load javascript before loading the html after the element and I don't think it's very wise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舟遥客 2024-12-07 22:21:11

您可以将 Javascript 代码直接放入元素文件中,或者将 Javascript 代码放入您的 webroot 文件夹 /app/webroot/js/ 中,并将该文件包含在您的布局中使用 HTML 帮助程序:

 echo $html->script("myCode");

如果您担心 Javascript 代码在页面完全加载之前执行,请使用 window.加载$(document).ready() 如果您使用的是 JQuery。

You could put the Javascript code directly into the element file, or put the Javascript code in into your webroot folder, <cake directory>/app/webroot/js/ and include the file in your layout by using the HTML helper:

 echo $html->script("myCode");

If you're worried about the Javascript code executing before the page has completely loaded, then use window.onload or $(document).ready() if you're using JQuery.

苦笑流年记忆 2024-12-07 22:21:11

如果我理解正确的话,当您调用某个元素时,您希望在标头中加载特定于页面的 JS,但每个元素的 JS 可能不同。您还希望在 HTML 文档的开头引用 JS。

这实际上很容易做到。只需确保您有 在您正在使用的布局中的 标记中。

然后,在该元素中,只需执行以下操作:

<?php $this->Html->script("js_file", array("inline"=>false)); ?>

js_file 是 app/webroot/js/ 中 JavaScript 文件的名称。在这种情况下,该文件将被称为 js_file.js,但在如上所述引用它时必须省略 .js。

将其放在元素文件中的哪个位置并不重要,因为 "inline"=>false 部分确保它实际上不会出现在代码的该阶段。相反,无论您在何处放置 中。 ?> 在您的布局中。

If I understand you correctly, you want to have JS specific to a page loading in the header when you call a certain element, but that the JS could be different for each element. You also want the JS to be referenced at the beginning of your HTML document.

This is actually quite easy to do. Just make sure that you have <?php echo $scripts_for_layout; ?> in you <head> tag in the layout you are using.

Then, within the element, just do:

<?php $this->Html->script("js_file", array("inline"=>false)); ?>

js_file is the name of your JavaScript file in app/webroot/js/. In this case, the file would be called js_file.js but you must leave off the .js when referencing it as above.

It doesn't matter where abouts in the element file you put this because the "inline"=>false part ensures it won't actually appear at that stage in the code. Instead, it will appear in the <head> wherever you put <?php echo $scripts_for_layout; ?> in your layout.

天涯沦落人 2024-12-07 22:21:11

在 cakephp 3 中,如果有人正在寻找类似的答案,则应该使用 array('inline' => false) 而不是 array('inline' => false)我是。

In cakephp 3 instead of array('inline' => false) you should use array('block' => true) if anyone is looking for that answer like I was.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文