QueryPath PHP 中的jQuery_PHP
jQuery
官方主页 http://querypath.org/
QP API 手册 http://api.querypath.org/docs/ 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:
复制代码 代码如下:
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
')// 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('
// Now go to the next row
->next()
// Add some data to this row
->append('
// Write it all out as HTML
->writeHTML();
?>
The code above produces the following HTML:
复制代码 代码如下:
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 它吧~~!

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

URL panjang, sering berantakan dengan kata kunci dan parameter penjejakan, boleh menghalang pelawat. Skrip pemendekan URL menawarkan penyelesaian, mewujudkan pautan ringkas yang sesuai untuk media sosial dan platform lain. Skrip ini sangat berharga untuk laman web individu a

Laravel memudahkan mengendalikan data sesi sementara menggunakan kaedah flash intuitifnya. Ini sesuai untuk memaparkan mesej ringkas, makluman, atau pemberitahuan dalam permohonan anda. Data hanya berterusan untuk permintaan seterusnya secara lalai: $ permintaan-

Ini adalah bahagian kedua dan terakhir siri untuk membina aplikasi React dengan back-end Laravel. Di bahagian pertama siri ini, kami mencipta API RESTful menggunakan Laravel untuk aplikasi penyenaraian produk asas. Dalam tutorial ini, kita akan menjadi dev

Laravel menyediakan sintaks simulasi respons HTTP ringkas, memudahkan ujian interaksi HTTP. Pendekatan ini dengan ketara mengurangkan redundansi kod semasa membuat simulasi ujian anda lebih intuitif. Pelaksanaan asas menyediakan pelbagai jenis pintasan jenis tindak balas: Gunakan Illuminate \ Support \ Facades \ http; Http :: palsu ([ 'Google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Pelanjutan URL Pelanggan PHP (CURL) adalah alat yang berkuasa untuk pemaju, membolehkan interaksi lancar dengan pelayan jauh dan API rehat. Dengan memanfaatkan libcurl, perpustakaan pemindahan fail multi-protokol yang dihormati, php curl memudahkan execu yang cekap

Adakah anda ingin memberikan penyelesaian segera, segera kepada masalah yang paling mendesak pelanggan anda? Sembang langsung membolehkan anda mempunyai perbualan masa nyata dengan pelanggan dan menyelesaikan masalah mereka dengan serta-merta. Ia membolehkan anda memberikan perkhidmatan yang lebih pantas kepada adat anda

Tinjauan Landskap PHP 2025 menyiasat trend pembangunan PHP semasa. Ia meneroka penggunaan rangka kerja, kaedah penempatan, dan cabaran, yang bertujuan memberi gambaran kepada pemaju dan perniagaan. Tinjauan ini menjangkakan pertumbuhan dalam PHP Versio moden

Dalam artikel ini, kami akan meneroka sistem pemberitahuan dalam rangka kerja web Laravel. Sistem pemberitahuan di Laravel membolehkan anda menghantar pemberitahuan kepada pengguna melalui saluran yang berbeza. Hari ini, kami akan membincangkan bagaimana anda boleh menghantar pemberitahuan ov
