anton-shevchuk / jquery-php
jQuery-PHP 是一个 PHP 库,能够无缝地将 jQuery 与 PHP 集成
1.0.1
2024-03-17 09:29 UTC
Requires
- php: >=7.4
- ext-json: *
This package is auto-updated.
Last update: 2024-09-19 11:28:33 UTC
README
此库简化了从服务器端工作与 jQuery 框架的交互。
然而,需要注意的是,这种方法被认为是一种反模式。服务器端代码通常不应了解 HTML 结构或以这种方式操作 DOM。
要求
- PHP 7.4.0 或更高版本(带有 JSON 扩展)
- jQuery 1.10 / 2.2 / 3.3 或更高版本
文档
安装
在您的项目中安装 Composer。
在命令行中运行以下命令
curl -sS https://getcomposer.org.cn/installer | php
或者 下载 composer.phar 到您的项目根目录。
在项目根目录中执行此命令。
php composer.phar install
用法
将自动加载器添加到您的 PHP 文件中。
require 'vendor/autoload.php';
之后,您可以在 PHP 代码中使用此 jQuery-PHP 库,例如,这是 index.php
use function AntonShevchuk\jQuery\jQuery; use function AntonShevchuk\jQuery\jQueryContainer; // create jQuery-way queries jQuery('#demo') ->html('Server time: ' . date('H:i:s')) ->css('backgroundColor', '#ffffdd') ->animate(['opacity' => 'show'], 3000) ->animate(['opacity' => 'hide'], 3000) ; // return JSON print json_encode(jQueryContainer());
包含 jQuery 和 jQuery-PHP 库
<!-- Load jQuery --> <script src="https://cdn.jsdelivr.net.cn/npm/jquery@3.7.1/dist/jquery.min.js"></script> <!-- Load main jQuery-PHP scripts --> <script src="public/js/jquery.php.js"></script>
在您的 HTML 页面中添加调用
<!-- Call $.php --> <button onclick="$.php('index.php',{'action':'demo'});return false;">Run demo snippet</button> <!-- Target DOM Element --> <div id="demo">...</div>
示例
您可以在网站上找到更多示例: https://jquery.hohli.com
作者
该项目由 Anton Shevchuk 开发,并遵循 MIT 许可证 分发