多个< head>和< body>标签 [英] Multiple <head> and <body> tag

查看:140
本文介绍了多个< head>和< body>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个非常简单的web应用程序,基本上是为了理解HTML5,CSS和JavaScript中的编码的最佳实践。



我的应用程序有3-4页面和每个使用相同的菜单标题。因此,我想通过将其写入单独的文件(PHP或HTML)中使其可重用。



head.php 可重用):

 <!DOCTYPE html> 
< html>
< head>
< link href =../../ css / headermenu.css =stylesheettype =text / css/>
< / head>
< body>
< ul id =menu>
< li>< a href =#class =home>首页< span>< / span>< / a>< / li&
< / ul>
< p>
< / p>
< / body>
< / html>

front.php:

 <?php 
include($ _SERVER [DOCUMENT_ROOT] /page/common/head.php);
?>

HTML标记(脏代码):

 <!DOCTYPE html> 
< html>
< head>
<!DOCTYPE html>
< html>
< head>
< link href =../../ css / headermenu.css =stylesheettype =text / css/>
< / head>
< body>
< ul id =menu>
< li>< a href =#class =home>首页< span>< / span>< / a>< / li&
< / ul>
< p>
< / p>
< / body>
< / HTML>< / head>
< body>
< div>
< / div>
< p>
< / p>
< / body>
< / html>

我有以下问题:


  1. head.php 同时拥有< body> < head> 标记。那么我应该在哪里写这些PHP行来包含它呢? (在< head> < body> )(我不想有多个< head> s和< body> s)


  2. 我应该遵循的其他最佳做法是什么? (任何链接到引用欢迎)


我已经读过w3schools。

解决方案

在我看来,阅读模板系统或看看frameworks / CMS如何处理这个是一个好主意。



按照你的方式,你不能完全避免重复eg每个content.php中的结束标记< / head>



一个想法:





$ b $ p $ < b

 <?php 
//其他包括/ requires,
//代码片段...

<!DOCTYPE html>
< html>
< head>

<! - 网站范围的样式表 - >
<! - & js-files - >
< link href =css / main.css =stylesheettype =text / css/>
< script type =text / javascriptsrc =my / global / scripts.js>< / script>

content.php

 <?php 
include($ _SERVER ['DOCUMENT_ROOT'] ./page/common/head.php');
?>

<! - 放页特定脚本&
<! - styles here - >
< link href =my / pages / css / style.css =stylesheettype =text / css/>
< script type =text / javascriptsrc =my / pages / js / scripts.js>< / script&
< / head>
< body>
< div id =container>
<! - content start - >
< div id =content>
< h1> title< / h1>
< p>您的内容< / p>
< / div>
<! - end of content div - >
<?php
include($ _SERVER ['DOCUMENT_ROOT']。'/page/common/foot.php');

foot.php

 < div id =foot> 
版权等
< / div>
< / div> <! - container of container div - >
< / body>
< / html>


I am trying to create a very simple web application, basically to understand the best practices of coding in HTML5, CSS and JavaScript.

My application has 3-4 pages and each one is using same menu header. So I want to make it reusable by writing it in a separate file (either PHP or HTML).

head.php (it is to be made reusable):

<!DOCTYPE html>
<html>
<head>
    <link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<ul id="menu">
    <li><a href="#" class="home">Home<span></span></a></li>
</ul>
<p> 
</p>
</body>
</html>

front.php:

<?php
    include ("$_SERVER[DOCUMENT_ROOT]/page/common/head.php");
?>

HTML markup (dirty code):

<!DOCTYPE html>
<html>
<head>
    <!DOCTYPE html>
<html>
<head>
    <link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<ul id="menu">
    <li><a href="#" class="home">Home<span></span></a></li>
</ul>
<p> 
</p>
</body>
</html></head>
<body>
    <div>
    </div>
    <p>
    </p>
</body>
</html>

I have following questions:

  1. head.php has both <body> and <head> tag. So where should I write these PHP lines to include it? (in <head> or <body>) (I don't want to have multiple <head>s and <body>s in the final page)

  2. What are the other best practice I should follow? (any link to references welcome)

I have already read w3schools.

解决方案

In my opinion it would be a good idea to read about templating systems or have a look how frameworks/CMS handle this.

Doing it your way, you can't completly avoid repeating e.g. the closing head tag </head> in every content.php.

So this is just an idea:

head.php

<?php
    // Some other includes / requires,
   // code snippets...
?>
<!DOCTYPE html>
<html>
    <head>

        <!-- site-wide stylesheets -->
        <!-- & js-files -->
        <link href="css/main.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="my/global/scripts.js"></script>

content.php

<?php
    include ($_SERVER['DOCUMENT_ROOT'] . '/page/common/head.php');
?>

    <!-- put page specific scripts &     
    <!-- styles here -->
    <link href="my/pages/css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="my/pages/js/scripts.js"></script>
</head>
<body>
    <div id="container">
        <!-- content start -->
        <div id="content">
            <h1>title</h1>
            <p>Your content</p>
        </div>
        <!-- end of content div -->
<?php
    include ($_SERVER['DOCUMENT_ROOT'] . '/page/common/foot.php');

foot.php

        <div id="foot">
               copyright etc
        </div> 
    </div> <!-- end of container div -->
</body>
</html>

这篇关于多个&lt; head&gt;和&lt; body&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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