
批改狀態(tài):合格
老師批語:關(guān)于網(wǎng)頁的導(dǎo)航一般不會寫固定, 數(shù)據(jù)從數(shù)據(jù)表中抓取, 前端遍歷,另外需要注意盡量使用foreach去遍歷php數(shù)組, 因?yàn)楫a(chǎn)生的是臨時(shí)變量, 遍歷結(jié)束后會被釋放, 相比于for遍歷數(shù)組來說速度更快~
<script>
let box = document.querySelector(".box");
//建立一個數(shù)組
let arr = ["寶馬", "奔馳", "奧迪", "雷克薩斯"];
//用forEach遍歷數(shù)組
arr.forEach((item) => {
box.append(item + "-");
});
//用for循環(huán)來遍歷數(shù)組
for (let i = 0; i < arr.length; i++) {
box.before(arr[i] + "-");
}
</script>
在php中
foreach
是小寫,第一個參數(shù)是數(shù)組對象
,第二個參數(shù)是鍵名(選填)
,第三個參數(shù)是遍歷的變量
,遍歷出來的數(shù)據(jù)保存在變量中
<?
//用$符號聲明一個變量。值是數(shù)組
$Arr = ["寶馬", "奔馳", "奧迪", "雷克薩斯"];
//讓后用for循環(huán)遍歷這個數(shù)組
for ($i=0; $i < count($Arr); $i++) {
echo $Arr[$i].'<br>';
}
echo '<hr>';
//在php中 foreach 是小寫,第一個參數(shù)是數(shù)組對象,第二個參數(shù)是鍵名(選填),第三個參數(shù)是遍歷的變量,遍歷出來的數(shù)據(jù)保存在變量中
foreach ($Arr as $key => $value) {
echo $key .'=>' .$value;
}
?>
大家可以看到,在這個頁面中我直接引入了另外一個文件,可以直接訪問到另外文件輸出的內(nèi)容,利用這個特點(diǎn)我們接下來模塊化開發(fā)一個共同的頁眉和頁腳吧
使用require模塊化引入php文件,使得代碼更加的簡潔,但是要提前設(shè)置好頁眉和頁腳,還有css樣式
/* 初始化 */
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: white;
}
li {
list-style: none;
margin: 0.5em;
height: 1.5em;
}
:root {
background: #ccc;
}
/*設(shè)置導(dǎo)航樣式*/
.thead {
position: fixed;
left: 0;
top: 0;
right: 0;
background-color: #000;
height: 40px;
}
.list {
display: flex;
justify-content: space-evenly;
align-items: center;
text-align: center;
}
.list > li:hover {
background-color: yellowgreen;
}
/*設(shè)置頁腳樣式*/
.tfoot {
position: fixed;
left: 0;
bottom: 0;
right: 0;
text-align: center;
background-color: #000;
color: white;
}
/*設(shè)置主體樣式*/
.main {
position: absolute;
top: 40px;
bottom: 1rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>模塊化開發(fā)頁眉頁腳</title>
<style>
@import url(/zwz/0425/style/style.css);
</style>
</head>
<body>
<!-- 頁眉 -->
<div class="thead">
<thead class="nav">
<ul class="list">
<li class='item' ><a href="" >首頁</a></li>
<li class='item' ><a href="">技術(shù)博客</a></li>
<li class='item' ><a href="">技術(shù)論壇</a></li>
<li class='item' ><a href="">我的博客</a></li>
<li class='item' ><a href="">會員中心</a></li>
</ul>
</thead>
</div>
<!-- 頁腳 -->
<div class="tfoot">
<tfoot>
<p class="copyright"><? echo '小張';?>© 版權(quán)所有</p>
</tfoot>
</div>
</body>
</html>
<!-- 引入頁眉 -->
<? require 'php/thead.php'?>
<!-- 主體 -->
<div class="main">
<ul>
<!-- <li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
<li>PHP</li> -->
</ul>
</div>
<!-- 引入頁腳 -->
<? require 'php/tfoot.php'?>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號