0

0

QueryPath PHP 中的jQuery

黄舟

黄舟

发布时间:2016-12-14 15:12:36

|

1330人浏览过

|

来源于php中文网

原创

querypath(qp)库 在 php 中实现了类似于 jquery 的效果,用它还可以方便地处理 xml html...功能太强大了!!!

A QueryPath Tutorial(一个简易说明) 
QueryPath makes use of method chaining to provide a concise suite of tools for manipulating a DOM. 
The basic principle of method chaining is that each method returns an object upon which additional methods can be called. In our case, the QueryPath object usually returns itself. 
Let's take a look at an example to illustrate: 
$qp = qp(QueryPath::HTML_STUB); // Generate a new QueryPath object.(创建一个 QP 对象) 
$qp2 = $qp->find('body'); // Find the body tag.(找到 "body" 标签) 
// Now the surprising part:(请看下面让你惊奇的地方) 
if ($qp === $qp2) { 
// This will always get printed.(它总是会这样输出) 
print "MATCH"; 

Why does $qp always equal $qp2? Because the find() function does all of its data gathering and then returns the QueryPath object. 
This might seem esoteric, but it all has a very practical rationale. With this sort of interface, we can chain lots of methods together: 
(你可以向使用 jQuery 一样来连缀方法) 
qp(QueryPath::HTML_STUB)->find('body')->text('Hello World')->writeHTML(); 
In this example, we have four method calls: 
qp(QueryPath::HTML_STUB): Create a new QueryPath object and provide it with a stub of an HTML document. This returns the QueryPath object. 
find('body'): This searches the QueryPath document looking for an element named 'body'. That element is, of course, the

portion of the HTML document. When it finds the body element, it keeps an internal pointer to that element, and it returns the QueryPath object (which is now wrapping the body element). 
text('Hello World'): This function takes the current element(s) wrapped by QueryPath and adds the text Hello World. As you have probably guessed, it, too, returns a QueryPath object. The object will still be pointing to the body element. 
writeHTML(): The writeHTML() function prints out the entire document. This is used to send the HTML back to the client. You'll never guess what this function returns. Okay, you guessed it. QueryPath. 
So at the end of the chain above, we would have created a document that looks something like this:

复制代码 代码如下:
 
 

 
 
Untitled 
 
Hello World 

Most of that HTML comes from the QueryPath::HTML_STUB. All we did was add the Hello World text inside of the

tags. 
Not all QueryPath functions return QueryPath objects. Some tools need to return other data. But those functions are well-documented in the included documentation. 
These are the basic principles behind QueryPath. Now let's take a look at a larger example that exercises more of the QueryPath API. 
A Longer Example 
This example illustrates various core features of QueryPath. 
In this example, we use some of the standard QueryPath functions (most of them implementing the jQuery interface) to build a new web page from scratch. 
Each line of the code has been commented individually. The output from this is shown in a separate block beneath.

复制代码 代码如下:
/** 
* Using QueryPath. 

* This file contains an example of how QueryPath can be used 
* to generate web pages. 
* @package QueryPath 
* @subpackage Examples 
* @author M Butcher  
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license. 
*/ 
// Require the QueryPath core. 
require_once 'QueryPath/QueryPath.php'; 
// Begin with an HTML stub document (XHTML, actually), and navigate to the title. 
qp(QueryPath::HTML_STUB, 'title') 
// Add some text to the title 
->text('Example of QueryPath.') 
// Now look for the element 
->find(':root body') 
// Inside the body, add a title and paragraph. 
->append('

This is a test page

Test text

立即学习PHP免费学习笔记(深入)”;

jQuery的左右侧边栏固定中间滚动代码
jQuery的左右侧边栏固定中间滚动代码

jQuery的左右侧边栏固定中间滚动代码

下载
') 
// Now we select the paragraph we just created inside the body 
->children('p') 
// Add a 'class="some-class"' attribute to the paragraph 
->attr('class', 'some-class') 
// And add a style attribute, too, setting the background color. 
->css('background-color', '#eee') 
// Now go back to the paragraph again 
->parent() 
// Before the paragraph and the title, add an empty table. 
->prepend('
') 
// Now let's go to the table... 
->find('#my-table') 
// Add a couple of empty rows 
->append(' ') 
// select the rows (both at once) 
->children() 
// Add a CSS class to both rows 
->addClass('table-row') 
// Now just get the first row (at position 0) 
->eq(0) 
// Add a table header in the first row 
->append('This is the header') 
// Now go to the next row 
->next() 
// Add some data to this row 
->append('This is the data') 
// Write it all out as HTML 
->writeHTML(); 
?>

The code above produces the following HTML:

复制代码 代码如下:
 
 

 
 
Example of QueryPath. 
 
 
 
 
 
This is the header
This is the data
 

This is a test page

 

Test text

 

Now you should have an idea of how QueryPath works. Grab a copy of the library and try it out! Along with the source code, you will get a nice bundle of HTML files that cover every single public function in the QueryPath library (no kidding). There are more examples there, too. 
不错的东东!赶紧 Grab 它吧~~!

感谢您的阅读,更多相关文章请关注PHP中文网(www.php.cn)!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
AO3官网入口与中文阅读设置 AO3网页版使用与访问
AO3官网入口与中文阅读设置 AO3网页版使用与访问

本专题围绕 Archive of Our Own(AO3)官网入口展开,系统整理 AO3 最新可用官网地址、网页版访问方式、正确打开链接的方法,并详细讲解 AO3 中文界面设置、阅读语言切换及基础使用流程,帮助用户稳定访问 AO3 官网,高效完成中文阅读与作品浏览。

89

2026.02.02

主流快递单号查询入口 实时物流进度一站式追踪专题
主流快递单号查询入口 实时物流进度一站式追踪专题

本专题聚合极兔快递、京东快递、中通快递、圆通快递、韵达快递等主流物流平台的单号查询与运单追踪内容,重点解决单号查询、手机号查物流、官网入口直达、包裹进度实时追踪等高频问题,帮助用户快速获取最新物流状态,提升查件效率与使用体验。

24

2026.02.02

Golang WebAssembly(WASM)开发入门
Golang WebAssembly(WASM)开发入门

本专题系统讲解 Golang 在 WebAssembly(WASM)开发中的实践方法,涵盖 WASM 基础原理、Go 编译到 WASM 的流程、与 JavaScript 的交互方式、性能与体积优化,以及典型应用场景(如前端计算、跨平台模块)。帮助开发者掌握 Go 在新一代 Web 技术栈中的应用能力。

11

2026.02.02

PHP Swoole 高性能服务开发
PHP Swoole 高性能服务开发

本专题聚焦 PHP Swoole 扩展在高性能服务端开发中的应用,系统讲解协程模型、异步IO、TCP/HTTP/WebSocket服务器、进程与任务管理、常驻内存架构设计。通过实战案例,帮助开发者掌握 使用 PHP 构建高并发、低延迟服务端应用的工程化能力。

4

2026.02.02

Java JNI 与本地代码交互实战
Java JNI 与本地代码交互实战

本专题系统讲解 Java 通过 JNI 调用 C/C++ 本地代码的核心机制,涵盖 JNI 基本原理、数据类型映射、内存管理、异常处理、性能优化策略以及典型应用场景(如高性能计算、底层库封装)。通过实战示例,帮助开发者掌握 Java 与本地代码混合开发的完整流程。

5

2026.02.02

go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

62

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

55

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

27

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
jQuery 教程
jQuery 教程

共42课时 | 5.3万人学习

HTML+CSS基础与实战
HTML+CSS基础与实战

共132课时 | 10.1万人学习

tp6+adminlte搭建通用后台
tp6+adminlte搭建通用后台

共39课时 | 5.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号