多个页面上有多个jQuery插件

时间:2014-10-15 16:06:23

标签: javascript php jquery

对于大多数人来说,我的问题可能非常简单。但我对jQuery很新,而且我在重复方面遇到了一些困难。

我在页面上有一个自定义脚本,该脚本包含大量文本,允许折叠该文本。这个脚本是在jQuery中。该页面是在PHP中。 我有这样的多个页面。我发现自己必须在每个页面上放置脚本文档的代码,如下所示:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
 <script type="text/javascript" src="javascripts/readmore.min.js"></script>
 <script src="javascripts/main_func.js"></script>

有没有办法可以将它只放在index.php文件中并调用脚本函数:

<script type="text/javascript">
    $(document).ready(function(){
        $('.article').readmore({maxHeight: 640});
    });
</script>

1 个答案:

答案 0 :(得分:0)

我不确定您的网页是如何加载的,但这是您必须要做的事情:

<强>的index.php

<?php
require('header.php');
require('content.php');
require('footer.php');
?>

<强>的header.php

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" >
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<meta name="viewport" content="width=device-width">
<title>Page Title</title>
<link rel="stylesheet" href="/css/default.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="javascripts/readmore.min.js"></script>
<script src="javascripts/main_func.js"></script>
</head>
<body>

<强> content.php

<div id="content">
<!-- Do display code here -->
</div>

<强> footer.php

<div id="footer">Footer Content</div>
</body>
</html>

如果你有一个新页面,它看起来像这样:

<强> newpage.php

<?php
// Keep same header
require('header.php');
// New content page
require('newcontent.php');
// Same footer
require('footer.php');
?>