Advertisement
fcamuso

Javascript Lezione 49

Jun 15th, 2022
1,441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="EN">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>DOM1</title>
  6.  
  7.     <link rel="stylesheet" href="style.css">
  8.  
  9.     <style>
  10.         img {width: 50px; height: 50px;}
  11.     </style>
  12.  
  13. </head>
  14. <body>
  15.     <div id='div1'>
  16.         <p id="p1">primo paragrafo</p>
  17.         <p id="p2">secondo paragrafo
  18.                    <img id="img1" src="teatro.png" alt="Locandina Teatro">
  19.         </p>
  20.     </div>
  21.  
  22.    
  23.  
  24.     <div id='div2'>
  25.         <a href="https://www.bbc.com">sito bbc</a>
  26.     </div>
  27.  
  28.     <script>
  29.         //RESTITUISCONO UN SOLO OGGETTO
  30.         //getElementById('questo_id') -> oggetto con quell'id
  31.         //querySelector('pattern_ricerca_alla_CSS') -> il primo che soddisfa il pattern
  32.  
  33.         //testo rosso e dimensione carattere 35px al testo del paragrafo id='p1'
  34.  
  35.         const par_p1 = document.getElementById('p1');
  36.        
  37.         //if (par_p1!==null){
  38.         if (!par_p1){ // "", '', undefined, false, NaN
  39.             //gestione errore
  40.         }
  41.         else
  42.         {
  43.             //  .style agisce  sugli stili IN LINEA
  44.             par_p1.style.fontSize = '2em';
  45.             par_p1.style.color = 'green';            
  46.         }
  47.  
  48.         const img1 = document.getElementById('img1');
  49.  
  50.         //getComputedStyle tiene conto di TUTTI gli stili
  51.         const stili_img1 = window.getComputedStyle(img1);
  52.        
  53.         const widthAttuale = parseInt(stili_img1.width); //da 50px al numero 50
  54.         const heightAttuale = parseInt(stili_img1.height);
  55.  
  56.         img1.style.width = widthAttuale*2+"px";
  57.         img1.style.height = heightAttuale*2+"px";
  58.  
  59.         //sovrascrive TUTTI gli stili in line
  60.         //par_p1.style.cssText = "background-color: blue; font-size: 40px";
  61.        
  62.         //NO! fuori standard:  par_p1.style = "background-color: blue; font-size: 40px";
  63.         Object.assign(par_p1.style, {backgroundColor: 'yellow', fontSize: '40px'});
  64.  
  65.         //stili embedded (sezione <style>)
  66.        
  67.         //caso <style> assente
  68.        
  69.         //a) creiamo il tag <style>
  70.         //const style = document.createElement('style');
  71.  
  72.         document.head.style.innerHTML += `
  73.           .p2 {background-color: lightgray;}
  74.           .banner {fontSize: 30px; color: silver;}
  75.           .ad {}
  76.         `
  77.  
  78.         //b) impostiamo le regole
  79.         // style.innerHTML = `
  80.         //   .p2 {background-color: lightgray;}
  81.         //   .banner {fontSize: 30px; color: silver;}
  82.         //   .ad {}
  83.         // `
  84.  
  85.         //c) innestiamo il tag dentro <head>
  86.         //document.head.appendChild(style);
  87.  
  88.         document.styleSheets[0].insertRule('body {border: 4px solid red;}',0);
  89.         document.styleSheets[0].deleteRule(1);
  90.        
  91.         // console.log(document.styleSheets[1]);
  92.  
  93.         //RESTITUISCONO UN INSIEME DI OGGETTI
  94.         //getElementsByName('questo_name')
  95.         //getElementsByTagName('questo_tag')
  96.         //getElementsByClassName('questa_classe')
  97.         //querySelectorAll('pattern_ricerca_alla_CSS')
  98.  
  99.     </script>
  100. </body>
  101. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement