【问题标题】:Magento 2 add class to body via Layout Update XMLMagento 2 通过布局更新 XML 将类添加到正文
【发布时间】:2017-03-15 14:07:43
【问题描述】:

如何通过布局更新 XML 将类添加到 CMS 页面的正文标记。 我正在尝试像这样的不同变体

<referenceContainer name="root">
  <attribute name="class" value="my-css-class"/>
</referenceContainer>

但没有任何效果。

【问题讨论】:

    标签: html xml magento magento2


    【解决方案1】:

    试试下面的代码。

    <body>
        <attribute name="class" value="my-css-class"/>
    </body>
    

    在 2.1.7 中测试并正常工作。

    【讨论】:

      【解决方案2】:

      我找到的唯一解决方案是向根容器添加一个类。 该元素将立即显示在 body 标记之后。

      我用过这段代码:

      <referenceContainer name="root" htmlTag="div" htmlClass="custom-class">
      </referenceContainer>
      

      我在 CMS 页面和类别的布局更新 XML 中工作。

      不幸的是,我找到的唯一解决方案类似于旧的“addBodyClass”。

      【讨论】:

        【解决方案3】:

        尝试在 xml 布局文件中添加这样的 css 或 js 文件:

        <?xml version="1.0"?>
        <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <head>
            <title>Page Title</title>
            <css src="Namespace_Module::css/styles.css"/>
            <script src="Namespace_Module::js/angular/angular.min.js"></script>
            <script src="Namespace_Module::js/app.js"></script>
        </head>
        <body>
            <referenceContainer name="content">
                <block class="Namespace\Module\Block\Class" name="Namespace_Module" template="Template.phtml"></block>
            </referenceContainer>
        </body>
        </page>
        

        然后在你的模块 web 文件夹(view/frontend/web)上,创建 js 和 css 文件夹并将你的文件放在那里。

        【讨论】:

          猜你喜欢
          • 2014-07-17
          • 1970-01-01
          • 1970-01-01
          • 2019-03-14
          • 1970-01-01
          • 2011-08-13
          • 1970-01-01
          • 1970-01-01
          • 2015-10-11
          相关资源
          最近更新 更多