IntroductionInstallationLimitations of the "file" protocolFrequently asked questionsSyntax, keywords and built-in functionsStandard distributionimport implementationBrython packagesBrowser interface
Introduction - DOM API
Creating a document Accessing elements Attributes, properties and methods Events Mouse events Keyboard events Focus events Drag events Query string Interactions with Javascript Brython-specific built-in modules
browser
browser.aio browser.ajax browser.html browser.local_storage browser.markdown browser.object_storage browser.session_storage browser.svg browser.template browser.timer browser.webcomponent browser.websocket browser.worker Widgets browser.widgets.dialog browser.widgets.menu interpreter javascript Working with BrythonCookbook |
Accessing elementsGetting access to an element can be done in different ways. The most usual is to use its identifier, ie its attribute id : with an input field defined by<input id="data">we can get a reference to this field by from browser import document data = document["data"] document is defined in module browser and refers to the HTML document.
It behaves like a dictionary whose keys are the identifiers of the elements in
the page. If no element has the specified id, the program raises a KeyError
exception.
All the elements in the page have a method get() that can be used to search
elements:
elt.select(S) is an alias for elt.get(selector=S) . A few examples :
document.select('.foo') # elements with class "foo" document.select('form') # list of elements "<form>" document.select('H1.bar') # H1 elements with class "bar" document.select('#container') # the element with id "container", same as # [document["container"]] document.select('a[title]') # A elements with an attribute "title" document.select('#tid td') # the TD elements inside the element with id #tidTo select a single element, use select_one() instead of select() . If the
selector matches several elements, select_one() returns the first one.
See the MDN documentation
for more information and examples of CSS selectors.
|