javascript 如何在 PHP 代码中插入 jQuery 插件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8284386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I insert a jQuery plugin into PHP code?
提问by law
I want to insert the jQuery plugin dropdown login formto make my website more pretty, but I am really confused where and how to insert jQuery into my code. For instance the following is my index.php
:
我想插入 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();
?>
And I want to add the jQuery plugin to the top of my index page.
我想将 jQuery 插件添加到我的索引页面的顶部。
How do I insert this jQuery plugin?
如何插入这个 jQuery 插件?
回答by Jhourlad Estrella
Get it straight from the CDN. Paste this code on your page's HEAD :
直接从 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>
Then utilize the library just as you would with your usual javascript code just like this one:
然后像使用常用的 javascript 代码一样使用该库,如下所示:
<script>
$(document).ready(function() {
alert('Hey! I was called when the document finished loading.');
});
</script>
回答by hungneox
I think you should echo the tag to your index page like this:
我认为您应该将标签回显到您的索引页面,如下所示:
echo "<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>"
Or you include an HTML/PHP file that contains jQuery script in the <head></head> tag
或者您在 <head></head> 标签中包含一个包含 jQuery 脚本的 HTML/PHP 文件
include('headScript.php');
Or you write a function that echo the <script></script> tag above:
或者你编写一个函数来回显上面的 <script></script> 标签:
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>';
}
回答by eciusr
Yes, just treat the JavaScript and jQuery code as you would HTML. The server reads the PHP code and the PHP code tells the server what to send to the browser, and the browser is what reads the jQuery and HTML.
是的,只需像对待 HTML 一样对待 JavaScript 和 jQuery 代码即可。服务器读取 PHP 代码,PHP 代码告诉服务器向浏览器发送什么,浏览器读取 jQuery 和 HTML。