PHP将样式表添加到标头 [英] PHP Adding stylesheets to header

查看:44
本文介绍了PHP将样式表添加到标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包含头文件之后,是否可以将样式表添加到头?说我们有以下代码:

Is there a way to add stylesheets to the header after including the header file? Say we have this code:

class content {
    public $stylesheets = array();

    public function addStylesheets($stylesheets)
    {
        if(!empty($stylesheets))
        {
            if(!is_array($stylesheets))
                $stylesheets = array($stylesheets);

            $this->stylesheets = array_merge($this->stylesheets, $stylesheets);
        }
    }

    public function getStylesheets()
    {
        $response = '';

        foreach($this->stylesheets as $stylesheet)
            $response .= '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '" />' . Chr(13);

        return $response;
    }
}

此标头:

<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="default.css" />
    <?php
        print($content->getStylesheets());
    ?>
</head>

<bod...

然后在文件中,我想添加如下样式表:

And then later in the file I want to add stylesheets like this:

$content->addStylesheets(array('home.css', 'slider.css'));

有人可以给我举个例子吗?

Can someone give me an example of how to do this?

推荐答案

由于@szapio已经很伤心,因此在最后构建HTML更有意义.

As @szapio already sad, it makes more sense to build the HTML at the very end.

无论如何,一种肮脏"的解决方案是通过将jQuery在</body> -标记之前(在< head> 中包含jQuery)之前:

Anyway, a "dirty" solution would be to hack your DOM with jQuery by putting the following before the </body>-tag (having jQuery included in the <head>):

<script>
$(document).ready(function() {
    $('head').append('<link rel="stylesheet" href="test.css" />');
});
</script>

您真的不应该考虑这样做,因为CSS文件将在其他所有内容之后加载,这意味着只要尚未加载CSS文件,您就会看到未样式化的元素……

You really shouldn't consider doing this, because the CSS file will be loaded after everything else, meaning you will see the unstyled elements as long as the CSS file is not loaded yet…

这篇关于PHP将样式表添加到标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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