File: attributes.rst

package info (click to toggle)
pyquery 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: python: 2,768; makefile: 128; xml: 9; sh: 4
file content (40 lines) | stat: -rw-r--r-- 831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Attributes
----------

..
    >>> from pyquery import PyQuery as pq

Using attribute to select specific tag
In attribute selectors, the value should be a valid CSS identifier or quoted as string::

    >>> d = pq("<option value='1'><option value='2'>")
    >>> d('option[value="1"]')
    [<option>]


You can play with the attributes with the jquery API::

    >>> p = pq('<p id="hello" class="hello"></p>')('p')
    >>> p.attr("id")
    'hello'
    >>> p.attr("id", "plop")
    [<p#plop.hello>]
    >>> p.attr("id", "hello")
    [<p#hello.hello>]


Or in a more pythonic way::

    >>> p.attr.id = "plop"
    >>> p.attr.id
    'plop'
    >>> p.attr["id"] = "ola"
    >>> p.attr["id"]
    'ola'
    >>> p.attr(id='hello', class_='hello2')
    [<p#hello.hello2>]
    >>> p.attr.class_
    'hello2'
    >>> p.attr.class_ = 'hello'