SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
Javascript : Manipuler le DOM
1. DOM ?
Document Object Model 
Le Document Object Model (ou DOM) est un standard du 
W3C qui décrit une interface indépendante de tout 
langage de programmation et de toute plate-forme, 
permettant à des programmes informatiques et à des 
scripts d'accéder ou de mettre à jour le contenu, la 
structure ou le style de documents XML et HTML 
http://fr.wikipedia.org
DOM & tree 
<!DOCTYPE html> 
<html> 
<head> 
<title>Exemple</title> 
</head> 
<body> 
<h1>Super titre</h1> 
<p>Super text</p> 
</body> 
</html> 
html 
head body 
title 
Exemple 
h1 
Super titre 
h1 
Super text
L’objet document 
Touts page web chargée dans un navigateur web a son 
propre objet document. Cet objet sert de point d'entrée 
au contenu de la page et apporte des fonctions générales 
au document. 
https://developer.mozilla.org
2. Sélection
Accéder aux éléments (1) 
var elementH1 = document.querySelector('h1'); 
console.log(typeof elementH1); // object 
console.log(elementH1); // <h1>Super titre</h1> 
var elementH2 = document.querySelector('h2'); 
console.log(typeof elementH2); // object 
console.log(elementH2); // null 
<h1>Super titre</h1> 
<p>Super text</p> 
<ul id="listTest"> 
<li>Test1</li> 
<li>Test2</li> 
<li>Test3</li> 
</ul>
Accéder aux éléments (2) 
var listElements = document.querySelectorAll('#listTest li'); 
console.log(typeof listElements); // object 
console.log(listElements); // [li, li, li, item: function] 
<h1>Super titre</h1> 
<p>Super text</p> 
<ul id="listTest"> 
<li>Test1</li> 
<li>Test2</li> 
<li>Test3</li> 
</ul>
3. Edition
Accéder aux éléments (1) 
var elementH1 = document.querySelector('h1'); 
elementH1.className = 'toto'; 
<h1 class="toto">Super titre</h1> 
<p>Super text</p> 
<ul id="listTest"> 
<li>Test1</li> 
<li>Test2</li> 
<li>Test3</li> 
</ul>
Accéder aux éléments (1) 
var listElements = document.querySelectorAll('#listTest li'); 
for (var i = 0; i < listElements.length; i++) { 
listElements[i]['id'] = 'list'+i; 
}; 
<h1>Super titre</h1> 
<p>Super text</p> 
<ul id="listTest"> 
<li id="list0">Test1</li> 
<li id="list1">Test2</li> 
<li id="list2">Test3</li> 
</ul>
3. Création
Créer un élément 
var newLiElement = document.createElement('li'); 
newLiElement.className = 'toto'; 
newLiElement.innerHTML = 'New Element'; 
<h1>Super titre</h1> 
<p>Super text</p> 
<ul id="listTest"> 
<li>Test1</li> 
<li>Test2</li> 
<li>Test3</li> 
</ul>
Ajouter un élément 
var newLiElement = document.createElement('li'); 
newLiElement.className = 'toto'; 
newLiElement.innerHTML = 'New Element'; 
var elementUl = document.querySelector('ul'); 
elementUl.appendChild(newLiElement); 
<h1>Super titre</h1> 
<p>Super text</p> 
<ul id="listTest"> 
<li>Test1</li> 
<li>Test2</li> 
<li>Test3</li> 
<li class="toto">New Element</li> 
</ul>
Merci pour votre attention.
Bibliographie 
Eloquent JavaScript - Marijn Haverbeke 
http://eloquentjavascript.net 
Guide JavaScript - teoli, BenoitL, delislejm, Ame_Nomade, SphinxKnight 
https://developer.mozilla.org/fr/docs/Web/JavaScript/Guide 
ECMAScript Language Specification - Ecma International 
http://ecma-international.org/ecma-262/5.1/ 
Javascript: The Definitive Guide - David Flanagan 
https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th

Más contenido relacionado

Destacado

Html & Css #5 : positionement
Html & Css #5 : positionementHtml & Css #5 : positionement
Html & Css #5 : positionementJean Michel
 
PHP & MYSQL #5 : fonctions
PHP & MYSQL #5 :  fonctionsPHP & MYSQL #5 :  fonctions
PHP & MYSQL #5 : fonctionsJean Michel
 
Une introduction à Javascript
Une introduction à JavascriptUne introduction à Javascript
Une introduction à JavascriptJean Michel
 
Startup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt meStartup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt meJean Michel
 
PHP #4 : sessions & cookies
PHP #4 : sessions & cookiesPHP #4 : sessions & cookies
PHP #4 : sessions & cookiesJean Michel
 
#3 html in the real world
#3 html in the real world#3 html in the real world
#3 html in the real worldJean Michel
 
Wordpress #2 : customisation
Wordpress #2 : customisationWordpress #2 : customisation
Wordpress #2 : customisationJean Michel
 
Javascript #3 : boucles & conditions
Javascript #3 : boucles & conditionsJavascript #3 : boucles & conditions
Javascript #3 : boucles & conditionsJean Michel
 
Les modèles économiques du web
Les modèles économiques du webLes modèles économiques du web
Les modèles économiques du webJean Michel
 
Dev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummiesDev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummiesJean Michel
 
Architecture logicielle #4 : mvc
Architecture logicielle #4 : mvcArchitecture logicielle #4 : mvc
Architecture logicielle #4 : mvcJean Michel
 
Html & Css #6 : formulaires
Html & Css #6 : formulairesHtml & Css #6 : formulaires
Html & Css #6 : formulairesJean Michel
 
Gestion de projet #4 : spécification
Gestion de projet #4 : spécificationGestion de projet #4 : spécification
Gestion de projet #4 : spécificationJean Michel
 
Javascript #2.2 : jQuery
Javascript #2.2 : jQueryJavascript #2.2 : jQuery
Javascript #2.2 : jQueryJean Michel
 
Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!Jean Michel
 
Javascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgmJavascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgmJean Michel
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événementsJean Michel
 
WebApp #2 : responsive design
WebApp #2 : responsive designWebApp #2 : responsive design
WebApp #2 : responsive designJean Michel
 
WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs Jean Michel
 
Architecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto frameworkArchitecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto frameworkJean Michel
 

Destacado (20)

Html & Css #5 : positionement
Html & Css #5 : positionementHtml & Css #5 : positionement
Html & Css #5 : positionement
 
PHP & MYSQL #5 : fonctions
PHP & MYSQL #5 :  fonctionsPHP & MYSQL #5 :  fonctions
PHP & MYSQL #5 : fonctions
 
Une introduction à Javascript
Une introduction à JavascriptUne introduction à Javascript
Une introduction à Javascript
 
Startup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt meStartup & entrepreneuriat #2.1: disrupt me
Startup & entrepreneuriat #2.1: disrupt me
 
PHP #4 : sessions & cookies
PHP #4 : sessions & cookiesPHP #4 : sessions & cookies
PHP #4 : sessions & cookies
 
#3 html in the real world
#3 html in the real world#3 html in the real world
#3 html in the real world
 
Wordpress #2 : customisation
Wordpress #2 : customisationWordpress #2 : customisation
Wordpress #2 : customisation
 
Javascript #3 : boucles & conditions
Javascript #3 : boucles & conditionsJavascript #3 : boucles & conditions
Javascript #3 : boucles & conditions
 
Les modèles économiques du web
Les modèles économiques du webLes modèles économiques du web
Les modèles économiques du web
 
Dev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummiesDev Web 101 #2 : development for dummies
Dev Web 101 #2 : development for dummies
 
Architecture logicielle #4 : mvc
Architecture logicielle #4 : mvcArchitecture logicielle #4 : mvc
Architecture logicielle #4 : mvc
 
Html & Css #6 : formulaires
Html & Css #6 : formulairesHtml & Css #6 : formulaires
Html & Css #6 : formulaires
 
Gestion de projet #4 : spécification
Gestion de projet #4 : spécificationGestion de projet #4 : spécification
Gestion de projet #4 : spécification
 
Javascript #2.2 : jQuery
Javascript #2.2 : jQueryJavascript #2.2 : jQuery
Javascript #2.2 : jQuery
 
Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!Javascript #5.1 : tp1 zombies!
Javascript #5.1 : tp1 zombies!
 
Javascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgmJavascript #4.2 : fonctions for pgm
Javascript #4.2 : fonctions for pgm
 
Javascript #8 : événements
Javascript #8 : événementsJavascript #8 : événements
Javascript #8 : événements
 
WebApp #2 : responsive design
WebApp #2 : responsive designWebApp #2 : responsive design
WebApp #2 : responsive design
 
WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs WebApp #4 : Consuming REST APIs
WebApp #4 : Consuming REST APIs
 
Architecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto frameworkArchitecture logicielle #5 : hipsto framework
Architecture logicielle #5 : hipsto framework
 

Similar a Javascript #7 : manipuler le dom

Intégration #2 : HTML 101 : Back to Basics
Intégration #2 : HTML 101 : Back to BasicsIntégration #2 : HTML 101 : Back to Basics
Intégration #2 : HTML 101 : Back to BasicsJean Michel
 
HTML 5 - intro - en francais
HTML 5 - intro - en francaisHTML 5 - intro - en francais
HTML 5 - intro - en francaisVlad Posea
 
Reseau et multimedia2009
Reseau et multimedia2009Reseau et multimedia2009
Reseau et multimedia2009jihen damerji
 
XML.pdf
XML.pdfXML.pdf
XML.pdfANBRI1
 
Document Object Model ( DOM)
Document Object Model ( DOM)Document Object Model ( DOM)
Document Object Model ( DOM)Abdelouahed Abdou
 
script site e-commerce -php
script site e-commerce -php script site e-commerce -php
script site e-commerce -php Yassine Badri
 
2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygen
2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygen2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygen
2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygenEmmanuelle Morlock
 
C5 Javascript French
C5 Javascript FrenchC5 Javascript French
C5 Javascript FrenchVlad Posea
 
HTML : why content is important - SoftShake 2014
HTML : why content is important - SoftShake 2014HTML : why content is important - SoftShake 2014
HTML : why content is important - SoftShake 2014Audrey Lièvremont
 
Formation - P2 - Standards du Web
Formation - P2 - Standards du WebFormation - P2 - Standards du Web
Formation - P2 - Standards du WebPatrick Vincent
 

Similar a Javascript #7 : manipuler le dom (20)

Intégration #2 : HTML 101 : Back to Basics
Intégration #2 : HTML 101 : Back to BasicsIntégration #2 : HTML 101 : Back to Basics
Intégration #2 : HTML 101 : Back to Basics
 
Introduction à jQuery
Introduction à jQueryIntroduction à jQuery
Introduction à jQuery
 
HTML 5 - intro - en francais
HTML 5 - intro - en francaisHTML 5 - intro - en francais
HTML 5 - intro - en francais
 
Reseau et multimedia2009
Reseau et multimedia2009Reseau et multimedia2009
Reseau et multimedia2009
 
XML.pdf
XML.pdfXML.pdf
XML.pdf
 
XML.pdf
XML.pdfXML.pdf
XML.pdf
 
Document Object Model ( DOM)
Document Object Model ( DOM)Document Object Model ( DOM)
Document Object Model ( DOM)
 
Mmi Web Design P2
Mmi Web Design P2Mmi Web Design P2
Mmi Web Design P2
 
HTML
HTMLHTML
HTML
 
script site e-commerce -php
script site e-commerce -php script site e-commerce -php
script site e-commerce -php
 
MMI Web Design P2
MMI Web Design P2MMI Web Design P2
MMI Web Design P2
 
2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygen
2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygen2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygen
2014 09 12_atelier-humanites-numerique-hisoma-seance-1-oxygen
 
Html
HtmlHtml
Html
 
C5 Javascript
C5 JavascriptC5 Javascript
C5 Javascript
 
C5 Javascript French
C5 Javascript FrenchC5 Javascript French
C5 Javascript French
 
HTML : why content is important - SoftShake 2014
HTML : why content is important - SoftShake 2014HTML : why content is important - SoftShake 2014
HTML : why content is important - SoftShake 2014
 
Formation - P2 - Standards du Web
Formation - P2 - Standards du WebFormation - P2 - Standards du Web
Formation - P2 - Standards du Web
 
Introduction au Jquery
Introduction au JqueryIntroduction au Jquery
Introduction au Jquery
 
XML
XMLXML
XML
 
Html css-xhtml
Html css-xhtmlHtml css-xhtml
Html css-xhtml
 

Más de Jean Michel

Startup #7 : how to get customers
Startup #7 : how to get customersStartup #7 : how to get customers
Startup #7 : how to get customersJean Michel
 
HTML & CSS #10 : Bootstrap
HTML & CSS #10 : BootstrapHTML & CSS #10 : Bootstrap
HTML & CSS #10 : BootstrapJean Michel
 
Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvasJean Michel
 
Architecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneArchitecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneJean Michel
 
Architecture logicielle #1 : introduction
Architecture logicielle #1 : introductionArchitecture logicielle #1 : introduction
Architecture logicielle #1 : introductionJean Michel
 
Wordpress #1 : introduction
Wordpress #1 : introductionWordpress #1 : introduction
Wordpress #1 : introductionJean Michel
 
PHP #2 : variables, conditions & boucles
PHP #2 : variables, conditions & boucles PHP #2 : variables, conditions & boucles
PHP #2 : variables, conditions & boucles Jean Michel
 
PHP #1 : introduction
PHP #1 : introductionPHP #1 : introduction
PHP #1 : introductionJean Michel
 
Startup #5 : pitch
Startup #5 : pitchStartup #5 : pitch
Startup #5 : pitchJean Michel
 
WebApp #1 : introduction
WebApp #1 : introductionWebApp #1 : introduction
WebApp #1 : introductionJean Michel
 

Más de Jean Michel (14)

Startup #7 : how to get customers
Startup #7 : how to get customersStartup #7 : how to get customers
Startup #7 : how to get customers
 
HTML & CSS #10 : Bootstrap
HTML & CSS #10 : BootstrapHTML & CSS #10 : Bootstrap
HTML & CSS #10 : Bootstrap
 
Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvas
 
Architecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezoneArchitecture logicielle #2 : TP timezone
Architecture logicielle #2 : TP timezone
 
Architecture logicielle #1 : introduction
Architecture logicielle #1 : introductionArchitecture logicielle #1 : introduction
Architecture logicielle #1 : introduction
 
Wordpress #1 : introduction
Wordpress #1 : introductionWordpress #1 : introduction
Wordpress #1 : introduction
 
PHP #6 : mysql
PHP #6 : mysqlPHP #6 : mysql
PHP #6 : mysql
 
PHP #2 : variables, conditions & boucles
PHP #2 : variables, conditions & boucles PHP #2 : variables, conditions & boucles
PHP #2 : variables, conditions & boucles
 
PHP #1 : introduction
PHP #1 : introductionPHP #1 : introduction
PHP #1 : introduction
 
Startup #5 : pitch
Startup #5 : pitchStartup #5 : pitch
Startup #5 : pitch
 
Projet timezone
Projet timezoneProjet timezone
Projet timezone
 
WebApp #3 : API
WebApp #3 : APIWebApp #3 : API
WebApp #3 : API
 
Projet timezone
Projet timezoneProjet timezone
Projet timezone
 
WebApp #1 : introduction
WebApp #1 : introductionWebApp #1 : introduction
WebApp #1 : introduction
 

Javascript #7 : manipuler le dom

  • 3. Document Object Model Le Document Object Model (ou DOM) est un standard du W3C qui décrit une interface indépendante de tout langage de programmation et de toute plate-forme, permettant à des programmes informatiques et à des scripts d'accéder ou de mettre à jour le contenu, la structure ou le style de documents XML et HTML http://fr.wikipedia.org
  • 4. DOM & tree <!DOCTYPE html> <html> <head> <title>Exemple</title> </head> <body> <h1>Super titre</h1> <p>Super text</p> </body> </html> html head body title Exemple h1 Super titre h1 Super text
  • 5. L’objet document Touts page web chargée dans un navigateur web a son propre objet document. Cet objet sert de point d'entrée au contenu de la page et apporte des fonctions générales au document. https://developer.mozilla.org
  • 7. Accéder aux éléments (1) var elementH1 = document.querySelector('h1'); console.log(typeof elementH1); // object console.log(elementH1); // <h1>Super titre</h1> var elementH2 = document.querySelector('h2'); console.log(typeof elementH2); // object console.log(elementH2); // null <h1>Super titre</h1> <p>Super text</p> <ul id="listTest"> <li>Test1</li> <li>Test2</li> <li>Test3</li> </ul>
  • 8. Accéder aux éléments (2) var listElements = document.querySelectorAll('#listTest li'); console.log(typeof listElements); // object console.log(listElements); // [li, li, li, item: function] <h1>Super titre</h1> <p>Super text</p> <ul id="listTest"> <li>Test1</li> <li>Test2</li> <li>Test3</li> </ul>
  • 10. Accéder aux éléments (1) var elementH1 = document.querySelector('h1'); elementH1.className = 'toto'; <h1 class="toto">Super titre</h1> <p>Super text</p> <ul id="listTest"> <li>Test1</li> <li>Test2</li> <li>Test3</li> </ul>
  • 11. Accéder aux éléments (1) var listElements = document.querySelectorAll('#listTest li'); for (var i = 0; i < listElements.length; i++) { listElements[i]['id'] = 'list'+i; }; <h1>Super titre</h1> <p>Super text</p> <ul id="listTest"> <li id="list0">Test1</li> <li id="list1">Test2</li> <li id="list2">Test3</li> </ul>
  • 13. Créer un élément var newLiElement = document.createElement('li'); newLiElement.className = 'toto'; newLiElement.innerHTML = 'New Element'; <h1>Super titre</h1> <p>Super text</p> <ul id="listTest"> <li>Test1</li> <li>Test2</li> <li>Test3</li> </ul>
  • 14. Ajouter un élément var newLiElement = document.createElement('li'); newLiElement.className = 'toto'; newLiElement.innerHTML = 'New Element'; var elementUl = document.querySelector('ul'); elementUl.appendChild(newLiElement); <h1>Super titre</h1> <p>Super text</p> <ul id="listTest"> <li>Test1</li> <li>Test2</li> <li>Test3</li> <li class="toto">New Element</li> </ul>
  • 15. Merci pour votre attention.
  • 16. Bibliographie Eloquent JavaScript - Marijn Haverbeke http://eloquentjavascript.net Guide JavaScript - teoli, BenoitL, delislejm, Ame_Nomade, SphinxKnight https://developer.mozilla.org/fr/docs/Web/JavaScript/Guide ECMAScript Language Specification - Ecma International http://ecma-international.org/ecma-262/5.1/ Javascript: The Definitive Guide - David Flanagan https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th