我设置模板php脚本以便轻松更改文件。我有index.php作为主页和两个php脚本footer.php和header.php。我在index.php中包含了其中两个。我的问题是可以从header.php访问footer.php中的html元素吗?
的index.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="header"><?php require "templates/header.php" ?></div>
<div class="footer"><?php require "templates/footer.php" ?></div>
</body>
</html>
的header.php
<!DOCTYPE html>
<html>
<head>
<script src="scripts/jquery.js"></script>
<script>
$(document).ready(function(){
$("#footerDiv").css('display','none');
});
</script>
</head>
<body>
</body>
</html>
footer.php
<!DOCTYPE html>
<html>
<head>
<script src="scripts/jquery.js"></script>
<script>
$(document).ready(function(){
$("#footerDiv").css('display','block');
});
</script>
</head>
<body>
<div id="footerDiv">
<p>I wanted to access this div from header.php</p>
</div>
</body>
</html>
答案 0 :(得分:0)
首先像这样创建index.php文件
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="header"><?php require "header.php" ?></div>
<div class="footer"><?php require "footer.php" ?></div>
</body>
</html>
然后像这样创建header.php文件
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#footerDiv").css('display','none');
});
</script>
然后像这样创建footer.php文件
<div id="footerDiv">
<p>I wanted to access this div from header.php</p>
</div>
最后运行index.php然后你可以看到页脚内容隐藏