Elements class

以下文档来自phpQuery Wiki



属性操作

Example

  1. QueryList::html($html)->find('a')->attr('href', 'newVal')->removeClass('className')->html('newHtml')->...

Table of Contents

Attr

  • attr($name) Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element. If the element does not have an attribute with such a name, undefined is returned.
  • attr($properties) Set a key/value object as properties to all matched elements.
  • attr($key, $value) Set a single property to a value, on all matched elements.
  • attr($key, $fn) Set a single property to a computed value, on all matched elements.
  • removeAttr($name) Remove an attribute from each of the matched elements.

Class

  • addClass($class) Adds the specified class(es) to each of the set of matched elements.
  • hasClass($class) Returns true if the specified class is present on at least one of the set of matched elements.
  • removeClass($class) Removes all or the specified class(es) from the set of matched elements.
  • toggleClass($class) Adds the specified class if it is not present, removes the specified class if it is present.

HTML

  • html() Get the html contents (innerHTML) of the first matched element. This property is not available on XML documents (although it will work for XHTML documents).
  • html($val) Set the html contents of every matched element. This property is not available on XML documents (although it will work for XHTML documents).

Text

  • text() Get the combined text contents of all matched elements.
  • text($val) Set the text contents of all matched elements.

Value

  • val() Get the content of the value attribute of the first matched element.
  • val($val) Set the value attribute of every matched element.
  • val($val) Checks, or selects, all the radio buttons, checkboxes, and select options that match the set of values.
    Read more at Attributes section on jQuery Documentation Site.


遍历操作

Example

  1. QueryList::html($html)->find('div > p')->add('div > ul')->filter(':has(a)')->find('p:first')->nextAll()->andSelf()->...

Table of Contents

Filtering

  • eq($index) Reduce the set of matched elements to a single element.
  • hasClass($class) Checks the current selection against a class and returns true, if at least one element of the selection has the given class.
  • filter($expr) Removes all elements from the set of matched elements that do not match the specified expression(s).
  • filter($fn) Removes all elements from the set of matched elements that does not match the specified function.
  • is($expr) Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
  • map($callback) Translate a set of elements in the jQuery object into another set of values in an array (which may, or may not, be elements).
  • not($expr) Removes elements matching the specified expression from the set of matched elements.
  • slice($start, $end) Selects a subset of the matched elements.

Finding

  • add($expr) Adds more elements, matched by the given expression, to the set of matched elements.
  • children($expr) Get a set of elements containing all of the unique immediate children of each of the matched set of elements.
  • contents() Find all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.
  • find($expr) Searches for all elements that match the specified expression. This method is a good way to find additional descendant elements with which to process.
  • next($expr) Get a set of elements containing the unique next siblings of each of the given set of elements.
  • nextAll($expr) Find all sibling elements after the current element.
  • parent($expr) Get a set of elements containing the unique parents of the matched set of elements.
  • parents($expr) Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element). The matched elements can be filtered with an optional expression.
  • prev($expr) Get a set of elements containing the unique previous siblings of each of the matched set of elements.
  • prevAll($expr) Find all sibling elements before the current element.
  • siblings($expr) Get a set of elements containing all of the unique siblings of each of the matched set of elements. Can be filtered with an optional expressions.

Chaining

  • andSelf() Add the previous selection to the current selection.
  • end() Revert the most recent 'destructive' operation, changing the set of matched elements to its previous state (right before the destructive operation).
    Read more at Traversing section on jQuery Documentation Site.


DOM操纵

Example

  1. QueryList::html($html)->find('div.old')->replaceWith( $ql->find('div.new')->clone() )->appendTo('.trash')->prepend('Deleted')->...

Table of Contents

Changing Contents

  • html() Get the html contents (innerHTML) of the first matched element. This property is not available on XML documents (although it will work for XHTML documents).
  • html($val) Set the html contents of every matched element. This property is not available on XML documents (although it will work for XHTML documents).
  • text() Get the combined text contents of all matched elements.
  • text($val) Set the text contents of all matched elements.

Inserting Inside

Inserting Outside

Inserting Around

  • wrap($html) Wrap each matched element with the specified HTML content.
  • wrap($elem) Wrap each matched element with the specified element.
  • wrapAll($html) Wrap all the elements in the matched set into a single wrapper element.
  • wrapAll($elem) Wrap all the elements in the matched set into a single wrapper element.
  • wrapInner($html) Wrap the inner child contents of each matched element (including text nodes) with an HTML structure.
  • wrapInner($elem) Wrap the inner child contents of each matched element (including text nodes) with a DOM element.

Replacing

Removing

  • empty() Remove all child nodes from the set of matched elements.
  • remove($expr) Removes all matched elements from the DOM.

Copying


form表单序列化

  • serialize() Serializes a set of input elements into a string of data. This will serialize all given elements.
  • serializeArray() Serializes all forms and form elements (like the .serialize() method) but returns a JSON data structure for you to work with.

例子:操作github登录表单

  1. $form = QueryList::get('https://github.com/login')->find('form');
  2. $form->find('input[name=login]')->val('github user');
  3. $form->find('input[name=password]')->val('github password');
  4. $data = $form->serialize();
  5. print_r($data);
  6. $data = $form->serializeArray();
  7. print_r($data);

输出:

  1. 0%5Bname%5D=utf8&0%5Bvalue%5D=%E2%9C%93&1%5Bname%5D=authenticity_token&1%5Bvalue%5D=EKXMN7xae%2BFZBv8r3y8qO2ICAwjAm9wfVt0S%2F9G7YuB2B7h%2Biw%3D%3D&2%5Bname%5D=login&2%5Bvalue%5D=github+user&3%5Bname%5D=password&3%5Bvalue%5D=github+password&4%5Bname%5D=commit&4%5Bvalue%5D=Sign+in
  2. Array
  3. (
  4. [0] => Array
  5. (
  6. [name] => utf8
  7. [value] =>
  8. )
  9. [1] => Array
  10. (
  11. [name] => authenticity_token
  12. [value] =>EKXMN7xae+FZBv8r3y8qO2ICAwjAm9wfVt0S/9G7YuB2B7h+iw==
  13. )
  14. [2] => Array
  15. (
  16. [name] => login
  17. [value] => github user
  18. )
  19. [3] => Array
  20. (
  21. [name] => password
  22. [value] => github password
  23. )
  24. [4] => Array
  25. (
  26. [name] => commit
  27. [value] => Sign in
  28. )
  29. )