小試QueryPath,一個實現了不少jQuery介面的PHP類庫

來源:互聯網
上載者:User

簡介:這是小試QueryPath,一個實現了不少jQuery介面的PHP類庫的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=338996' scrolling='no'>

很想寫一個PHP的jQuery,但是自己精力與能力有限,於是Google一下,找到 QueryPath。

其實前幾天已經找到一個,叫 simple_html_dom (SourceForge下載),不到1000行代碼,很簡單。有興趣就下載看看。

基本使用方法:

require 'src/QueryPath/QueryPath.php';
// 解釋HTML為DOM
qp('<html>...</html>');

// 或者負載檔案
qp('http://www.google.com.hk/index.html');

如果 qp的第一個參數是 url(包括http、file),則需要以html或htm為尾碼名,否則當作XML來解釋,通常會解釋失敗,並拋出 QueryPathExtension 異常,這應該說是一個缺陷,2.1版本的代碼在QueryPath.php的3903-4010行。

有了qp返回的對象,我們就可以用 PHP 以 jQuery 類似的方法來操作DOM,如選擇節點,可以用CSS3選取器、parent/top/children等函數。

下載是官方手冊上的一個快速入門的例子 :

<?php
require_once '../src/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 <body> element
  ->find(':root body')
  // Inside the body, add a title and paragraph.
  ->append('<h1>This is a test page</h1><p>Test text</p>')
  // 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('<table id="my-table"></table>')
  // Now let's go to the table...
  ->find('#my-table')
  // Add a couple of empty rows
  ->append('<tr></tr><tr></tr>')
  // 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('<th>This is the header</th>')
  // Now go to the next row
  ->next()
  // Add some data to this row
  ->append('<td>This is the data</td>')
  // Write it all out as HTML
  ->writeHTML();
?>

詳細請參考其官網及API手冊(包含在源碼中):http://querypath.org/

QueryPath的基本特徵:http://querypath.org/node/2

愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具

http://biancheng.dnbcw.info/php/338996.html pageNo:8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.