我有两个div,它们正确地并排浮动,但在如下嵌入php代码后,整个页面将不会显示任何内容。即使我删除了php代码,页面也不会再次显示。下面的代码适用于向右浮动的div。
<div style="float:right;">
<?php
echo "<div>
<head>
<link rel='stylesheet' type='text/css' href='chatbox/style/cb_style.css'>
<script type='text/javascript' src='chatbox/ajax.js'></script>
<script type='text/javascript' src='chatbox/chatbox.js'></script>
</head>
<div>";
?>
<div id="container">
<div id="usersOnLine">
<div style="font-weight:bold;margin-top:35%;">Online Users</div>
<div style="height:82%;">
<?php
if (!empty($friends)) {
foreach ($friends as $friend) {
$friend_name = htmlentities($user['username'], ENT_NOQUOTES);
echo "<span>{$friend_name}</span>";
}
}
?>
</div>
<form method="post" action="">
<input type='text' placeholder='search' style='width:145px;'/><a href='#'><span class='glyphicon glyphicon-search'></span></a>
</form>
</div>
<div id="chat_search">
<un>
</div>
</div>
</div>
<div style="clear:both;"></div>
由于要将<head>
元素添加到DOM中,因此应该删除div
和head
周围的元素,并将第一块php代码放在页面的<head>
部分中。当您以这种方式引入新的<head>
元素时,浏览器会感到困惑。
编辑:用一个可能的例子来进一步解释。。。
您可以将这些新的样式表和js条目添加到head
中,方法是将它们附加到您的头include中(假设您对主html头使用include-在本例中,我们称之为"header.php",因为这很常见)
<?php
$addme = '<link rel="stylesheet" type="text/css" href="chatbox/style/cb_style.css">
<script type="text/javascript" src="chatbox/ajax.js"></script>
<script type="text/javascript" src="chatbox/chatbox.js"></script></head>';
ob_start(); // start a buffer
include("header.php");
$contents = ob_get_contents(); // buffer the contents of header.php
ob_end_clean(); // end & clean the buffer
// replace the closing tag with the added stuff
echo str_replace('</head>', $addme, $contents);
?>