【问题标题】:how to access an element that included using php file如何访问包含使用 php 文件的元素
【发布时间】:2017-08-25 13:01:06
【问题描述】:

我设置模板 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>

页脚.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>

【问题讨论】:

标签: php


【解决方案1】:

首先像这样创建 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 然后你可以看到页脚内容隐藏了

【讨论】:

  • 我添加了一个代码,请你看到它并说点什么#Shafiqul
  • 你想访问意味着你想从标题中获取文本?
  • 是的一切,就像我在 header.php 中所做的那样,我想更改 div 的样式,当我运行 index.php 时,我已经尝试过但不适合我。
  • 对不起,我没有得到你所做的改变,一切都和我发布的一样……你能帮我描述一下吗……
  • 我已经测试过并且工作正常,我相信它会工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
相关资源
最近更新 更多