是否有一种方法可以在HTML的主体中使用PHP来“移动”它


Is there a way to write styling within the body of HTML using PHP to *Move* it

我只是想知道是否有一种方法可以在body内编写CSS样式,使用一些php将其拉入head标签。

…不一定是内联样式

…是否有一种方法来运行php之前的HTML头标签被解析?

我认为这是可能的:

  1. 在正文中写一些样式:

    <body>
      <div class="moveCSS">
        <style type="text/css">
          #frog
          {
          font-color: green;
          }
        </style>
      </div>
    <p class="frog">Frog's are stereotypically documented as being 'green' in colour, when in actual fact are often very different depending on their environmental habitat!</p>
    </body>
    
  2. 在头部写类似这样的内容:

    <head>
      <?php
        function getElementsByClassName('DOMDocument $DOMDocument, $ClassName)
        {
          $Elements = $DOMDocument->getElementsByTagName("moveCSS");
          $Matched = array();
          for($i=0;$i<$Elements->length;$i++)
          {
            if($Elements->item($i)->attributes->getNamedItem('class')->nodeValue == $ClassName)
            {
              $Matched[]=$Elements->item($i);
            }
          }
          return $Matched;
        }
    ?>
    </head>
    

    (参考PHP代码片段:Link to Page…仅供参考:我确信变量名需要在PHP中以小写字母字符开头)

如果这是可能的,它将允许我在CMS文章中编写更好的CSS,避免访问CSS文件和手动添加自定义条目的要求。'moveCSS'的单一类名将强制在'moveCSS'div中写入的任何内容都将包含在头部。让我的作品更具可塑性

不,而是在原始文档中编写css,并在需要的地方用JavaScript加载它,或者如果css不太复杂,则完全内联编写。