如何将脚本和CSS挂接到< head>中? [英] How can I hook on scripts and CSS into <head>?

查看:76
本文介绍了如何将脚本和CSS挂接到< head>中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作的模块的事情是,它会生成一个javascript代码段,因此我不能使用操作将其挂接到HTML部分中,因为该操作需要我有一个JS文件(请纠正我如果我错了).我可以通过哪些方式将JavaScript代码段添加到代码中?我当时在考虑使用一个块,但是我不确定该块后面应该附加什么,因此我不得不考虑这将适用于所有主题.

The thing with the module I am making is that it kind of generates a javascript snippet, so I cannot use an action to just hook that into the section of the HTML since the action requires that I have a JS file (correct me if I am wrong). What are some ways for me to put a JavaScript snippet into the tag? I was thinking of using a block, but I am not sure what the block should be appended after and I have to consider that this will work with all themes.

推荐答案

库存模板是

template/page/html/head.phtml

以您自己的主题复制该文件将是获得一些javascript的最简单方法.

Copying that file in your own theme would be the simplest way to get some javascript in the head.

尽管(从开发人员的角度来看)更好,但是此模板包括以下行

Better though (from a developer point of view), this template includes the following line

<?php echo $this->getChildHtml() ?>

关于链接将打印出一个块的所有子块.因此,将一个子块添加到head块也将起作用.

The about link prints out all the child blocks of a block. So, adding a child block to the head block would also work.

<layouts>
    <default> <!-- does this to all pages — use specific layout handles to target a page -->
        <reference name="head"> <!-- get a reference to the existing head block -->
            <block type="core/text" name="simple_example_javascript_block"> <!-- append a simple text block, probably better to use a new template block -->
                <action method="setText"> <!-- set our new block's text -->
                    <text><![CDATA[
                    <script type="text/javascript">
                        alert("foo");
                    </script>
                    //]]></text>
                </action>
            </block>
        </reference>
    </default>
</layouts>

上面的XML使用简单的core/text块将javascript添加到每个Magento页面. local.xml中的作品,应该在其他地方工作.我敢肯定,可以想到更好的方法(例如,模板块)

The above XML uses a simple core/text block to add javascript to every Magento page. Works from local.xml, should work elsewhere. I'm sure better ways to do this should spring to mind (template block, for example)

这篇关于如何将脚本和CSS挂接到&lt; head&gt;中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆