Preface: When a web page is loaded, the browser creates 1. JavaScript can change all HTML elements in the page1. Find HTML element by idFind a specific HTML tag and modify it <body> <div calss="001">Who am I</div> <script> var x = document.getElementById("001"); x.innerHTML = "I am a brave bull, not afraid of difficulties"; </script> </body> <body> <p id="intro">Hello Niu Niu!</p> <p>This example demonstrates the <b>getElementById</b> method!</p> <script> x=document.getElementById("intro"); document.write("<p>Text from paragraph with id intro: " + x.innerHTML + "</p>"); </script> </body> 2. Find HTML elements by tag namevar x = document.getElementById("main"); var y=x.getElementsByTagName("p"); document.write('The first paragraph in the id="main" element is:' + y[0].innerHTML); Find the element with //The first way to write it var x = document.getElementById("main"); var y = x.getElementsByTagName("p")[0]; //Find all the p tags in HTML and replace the first tag content var y = document.getElementsByTagName("p"); y[0].innerHTML = "Brave Bull, not afraid of difficulties" <script> var x = document.getElementById("main"); var y=x.getElementsByTagName("p"); document.write('The first paragraph in the id="main" element is:' + y[0].innerHTML); </script> 3. Find HTML elements by class name<body> <p class="intro">Hello Niu Niu!</p> <p>This example demonstrates the <b>getElementsByClassName</b> method!</p> <script> x = document.getElementsByClassName("intro"); document.write("<p>Text from class intro paragraph: " + x[0].innerHTML + "</p>"); </script> </body> Modify the element content of such tags: <body> <p class="intro">Hello Niu Niu!</p> <p>This example demonstrates the <b>getElementsByClassName</b> method!</p> <script> x = document.getElementsByClassName("intro")[0]; x.innerHTML = "Modify tag content"; // document.write("<p>Text from class intro paragraph: " + x[0].innerHTML + "</p>"); </script> </body> 4. JavaScript can change all HTML attributes in the pageTo change the attributes of an HTML element, use this syntax: document.getElementById(id).attribute=new attribute value <img id="image" src="smiley.gif" width="160" height="120"> <script> document.getElementById("image").src="landscape.jpg"; </script> <p>The original image is smiley.gif, the script changes the image to landscape.jpg</p> 5. JavaScript can change all CSS styles on the page
To change the style of an HTML element, use this syntax: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>title</title> </head> <body> <p id="p1">Hello World!</p> <p id="p2">Hello World!</p> <script> document.getElementById("p2").style.color="blue"; document.getElementById("p2").style.fontFamily="Arial"; document.getElementById("p2").style.fontSize="larger"; </script> <p>The above paragraphs are modified by script. </p> </body> </html> 6. JavaScript can respond to all events on the pageHTML DOM to dispatch events:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p>Clicking the button executes the <em>displayDate()</em> function. </p> <button id="myBtn">Click me</button> <script> document.getElementById("myBtn").onclick=function(){ displayDate()}; function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> <p id="demo"></p> </body> </html> as follows: The onmouseover and onmouseout events can be used to trigger functions when the mouse pointer moves to or leaves an element. <!DOCTYPE html> <html><head> <meta charset="utf-8"> </head> <body> <div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:#D94A38;width:120px;height:20px;padding:40px;">Mouse Over Me</div> <script> function mOver(obj) { obj.innerHTML="Thank You" } function mOut(obj) { obj.innerHTML="Mouse Over Me" } </script> </body> </html> This is the end of this article about JavaScript Document Object Model DOM. For more relevant JavaScript Document Object Model content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: What is the function and writing order of the a tag pseudo class
>>: Design perspective technology is an important capital of design ability
Preface In WEB development, we often involve cros...
1. The ul tag has a padding value by default in M...
Understanding object.defineProperty to achieve re...
This article example shares the specific code of ...
Table of contents background Purpose Before split...
I just started learning about databases recently....
1. The vertical-align property achieves the follo...
Purpose Understand the Nginx ngx_http_limit_conn_...
Table of contents JSON appears Json structure Jso...
As shown below: select a1,a2,a1+a2 a,a1*a2 b,a1*1...
This article example shares the specific code of ...
Introduction to IPSec IPSec (Internet Protocol Se...
View the nginx configuration file path Through ng...
1. Introduction Recently I found that there are m...
There is such a requirement: an import button, cl...