如何在PHP代码中插入jQuery插件?

How can I insert a jQuery plugin into PHP code?

本文关键字:插入 jQuery 插件 代码 PHP      更新时间:2023-09-26

我想插入jQuery插件下拉登录表单使我的网站更漂亮,但我真的很困惑在哪里以及如何插入jQuery到我的代码。例如,下面是我的index.php:

<?php
    include ('book_fns.php');
    session_start();
    do_html_header("Welcome to Store");
    echo "<p>Please choose a category:</p>";
    // Get categories out of database
    $cat_array = get_categories();
    // Display as links to cat pages
    display_categories($cat_array);
    //If logged in as admin, show add, delete, edit cat links.
    if(isset($_SESSION['admin_user'])) {
        display_button("admin.php", "admin-menu", "Admin Menu");
    }
    do_html_footer();
?>

我想把jQuery插件添加到我的索引页的顶部。

如何插入这个jQuery插件?

直接从CDN获取。将此代码粘贴到页面的HEAD:

<HTML>
   <HEAD>    
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
   </HEAD>
</HTML>

然后使用库,就像你通常使用javascript代码一样,就像这样:

<script>
    $(document).ready(function() {
      alert('Hey! I was called when the document finished loading.');
    });
</script>

我认为你应该像这样在你的索引页上回复标签:

echo "<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>"

或者在

标签 中包含包含jQuery脚本的HTML/PHP文件
include('headScript.php');

或者你可以写一个函数来回应上面的标签:

echo $this->headScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
protected function headScript($src){
    return '<script type="text/javascript" src="$src"></script>';
}

是的,就像对待HTML一样对待JavaScript和jQuery代码。服务器读取PHP代码,PHP代码告诉服务器发送给浏览器的内容,浏览器读取jQuery和HTML。