Phpquery Chinese Handbook (updated)

Source: Internet
Author: User
Tags prev

At present, there are few Chinese documents on the Internet phpquery, and the official documents are on Google, and some commonly used phpquery methods are compiled for everyone to consult and beginners to learn.

The following information is from official documents, Official document address: Https://code.google.com/archive/p/phpquery/wikis base Sample

phpquery::newdocumentfilexhtml (' my-xhtml.html ')->find (' P '); $ul = PQ (' ul '); Load Document phpquery:: newdocument ($html, $contentType = null) creates a new document based on the tag URL. If the   $contentType is empty, the encoding is automatically detected from the document. Detection fails, the UTF-8 encoding is automatically assigned to the Text/html type document. Phpquery:: newdocumentfile ($file, $contentType = null) creates a new document from the file. Similar to NewDocument () Phpquery:: newdocumenthtml ($html, $charset = ' utf-8 ') Phpquery:: Newdocumentxhtml ($html, $charset = ' utf-8 ') Phpquery:: newdocumentxml ($html, $charset = ' Utf-8 ') Phpquery:: newdocumentphp ($html, $contentType = null)   Phpquery:: Newdocumentfilehtml ($file, $charset = ' utf-8 ') Phpquery:: newdocumentfilexhtml ($file, $ CharSet = ' Utf-8 ') Phpquery:: newdocumentfilexml ($file, $charset = ' utf-8 ') Phpquery:: Newdocumentfilephp ($file, $contentType)   PQ function 

PQ ($param, $context = null);

PQ (); Equivalent to jquery $ ();. It mainly accomplishes three things:

1. Load Tag resources:

Enter the document to load:
A node that does not receive text types for the first input string: PQ (' <div/> ')
Load to document from $pq->getdocumentid () by ID: PQ (' <div/> ', $pq->getdocumentid ())
Load the same document according to the ownership of the DOM node: PQ (' <div/> ', Domnode)
Load document from Phpquery object: PQ (' <div/> ', $PQ)

2. Run the query

To execute a query based on the last selected document: PQ (' Div.myclass ')
Query from the document based on the ID of $pq->getdocumentid (): PQ (' Div.myclass ', $pq->getdocumentid ())
Queries on the same document based on the ownership of the DOM node and uses the node as the root node of the query: PQ (' Div.myclass ', Domnode)
Querying with Phpquery objects on a document
Use the stack of objects as the root node to query: PQ (' Div.myclass ', $PQ)

3. Using the Phpquery object to prototype a DOM node

foreach (PQ (' Li ') as $li)//$li is a pure DOM node, turning it into a Phpquery object: PQ ($li); selector

Selectors are a core similar to the jquery interface. Most CSS3 syntax has been adopted (and jquery is kept in sync). Sample

PQ (". Class ul > li[rel= ' foo ']:first:has (A)")->appendto ('. Append-target-wrapper div '). Base

#id matches a single element based on the given ID attribute. element matches all conforming elements according to the given name. . Class matches all the elements according to the given class. * Select all elements.

Selector1, Selector2, Selectorn match the results according to all established selectors. level

ancestor descendant matches all descendant elements specified by the descendants of the elements specified by the ancestors. Parent > Child match all child elements specified by the parent element. prev + next matches all the next elements according to the specified "next" and the specified "prev".

prev ~ siblings matches all adjacent elements based on the "prev" element. Basic Filtration

: first matches the selected element. : Last matches the final selected element. : Not (selector) matches all elements that are not selected. : Even matches all the selected even elements, 0 indexes. : Odd matches all selected odd elements, 0 index. : eq (index) matches the element that is equivalent to the given index. : GT (Index) matches an element that is greater than the given index. : LT (index) matches an element that is less than the given index. : Header matches all header elements, such as H1,H2,H3.

: Animated matches the element in which the animation effect is being performed. Text Filtering

: Contains (text) matches the element that contains the specified text. : Empty matches all elements that have no child nodes (including text nodes). : The has (selector) match contains at least one element for the given selector. :p arent matches all parent elements-the owning child element, including the text. attribute Filtering [attribute] matches the element of the given property. [Attribute=value] matches the element with the given property equal to the determined value. [Attribute!=value] matches the element for which the given property is not equal to the determined value. [Attribute^=value] matches the given property as the element that determines the start of the value. [Attribute$=value] matches the given property as the element that determines the end of the value. [Attribute*=value] matches the element that contains the specified value for the given property.

[Selector1selector2selectorn] matches the given property and contains elements that determine the value. child element Filtering

: Nth-child (index/even/odd/equation) matches all the nth child elements of the parent element, or even or odd child elements of the parent element. : First-child matches all child elements that are the first of the parent element. : Last-child matches all child elements that are the last of the parent element.

: Only-child matches all child elements that are unique child elements of the parent element. form

: Input matches input, textarea, select, and button elements. : Text matches all input elements of type text. :p assword matches all input elements of type password. : Radio matches all input elements of type radio. : The checkbox matches all input elements of type checkbox. : Submit matches all input elements of type submit. : Image matches all input elements of type image. : Reset matches all input elements of type reset. : The button matches the INPUT element and the button element for all types of button. : File matches all input elements of type file.

: Hidden matches all input elements or other hidden elements of type hidden. Form Filter

: Enabled to match all available elements. :d isabled matches all unavailable elements. : Checked matches all the checked elements. : Selected matches all selected elements. Method Sample

PQ (' a ')->attr (' href ', ' newval ')->removeclass (' ClassName ')->html (' newhtml '). Attr

attr ($name) To access the properties of the first element that gives the name. This method makes it easy to get the property value of the first matching element. Returns undefined if the element does not have an attribute of the corresponding name. attr ($properties) sets the corresponding property for all matching elements. attr ($key, $value) sets a property and corresponding value for the element that is matched to. attr ($key, $FN) sets a property and the value to be evaluated for the element that is matched.

removeattr ($name) A property that removes the given name from the element to which it is matched. Class

AddClass ($class) To add a given class to the element to which it is matched. Hasclass ($class) Returns true if at least one of the matched elements contains a given class. Removeclass ($class) A class that removes the given name from the element to which it is matched.

Toggleclass ($class) to the element that is matched to, if the class does not exist, to add, or remove if it exists. HTML

HTML () Gets the HTML content (InnerHTML) of the first element to match. This method does not apply to XML literals (but applies to XHTML.) )

HTML ($val) to set the HTML content for the matching element. This method does not apply to XML literals (but applies to XHTML.) ) Text

text () Gets the textual content of all the elements that are matched to.

text ($val) Sets the text content for all elements that are matched to. Value

Val () Gets the value of the Value property of the first element that is matched to. Val ($val) Sets the value value for the element that is matched to. Val ($val) All checks, selects, radio buttons, checkboxes, and select Options all set the corresponding values.

The above is Phpquery basic operation method, everybody has what want to continue to understand please leave a message, I will take time to update.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.