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不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

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

2

2026.01.31

go语言 math包
go语言 math包

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

1

2026.01.31

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

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

1

2026.01.31

golang 循环遍历
golang 循环遍历

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

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

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

精品课程

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

共42课时 | 5.2万人学习

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

共132课时 | 10万人学习

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号