【问题标题】:Add javascript code in payment.phtml in the Magento在 Magento 的 payment.phtml 中添加 javascript 代码
【发布时间】:2013-02-02 13:12:15
【问题描述】:

我有以下布局 xml

<reference name="content">
    <block type="gate/paycheckout" name="payair.gate.paycheckout" before="-" template="gate/checkout.phtml"/>
    <block type="gate/paymentjs" name="payair.gate.paymentjs" />
</reference>

我有一个块,我通过它调用 payment.phtml 上的 javascript 及其工作,但我想调用 Magento addJs 操作方法以在 payment.phtml 页面上动态添加 javascript,而不是通过调用块。我该怎么做?

【问题讨论】:

    标签: magento action


    【解决方案1】:

    您应该将脚本添加到 head:

    布局xml文件:

    <layout>
      <name_of_the_handle>
        <reference name="head">
          <action method="addJs"><script>gate/payment.js</script></action>
        </reference>
      </name_of_the_handle>
      ...
    </layout>
    

    payment.js 文件在这种情况下应该位于 js/gate/payment.js

    【讨论】:

    • 我不想专门在头部添加js。只想在payment.phtml中添加..谢谢
    • 如果您在 Magento 源代码中搜索 addJs 函数,您会看到该函数仅存在于 Mage_Page_Block_Html_Head 中,因此您将无法在其他模块中使用它,除非您自己在您的自己的块。您应该只使用上面的代码并将 name_of_the_handle 替换为仅在呈现 payment.phtml 文件时存在的句柄名称。
    • 那么参考名称是什么?你能建议我吗?我正在尝试这样... ...
    • 参考名称="head"。 name 属性是应该调用函数的块的名称。 action 元素中的method 属性值实际上是应该调用的类函数的名称。因此,由于 addJs 函数仅存在于 Mage_Page_Block_Html_Head 块中,因此您应该引用该块。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多