亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Introduction to JavaScript DOM

What is Dom?

The Document Object Model (DOM) is a standard programming interface recommended by the W3C organization for processing extensible markup languages. The history of the Document Object Model can be traced back to the "browser war" between Microsoft and Netscape in the late 1990s. In order to compete for life and death in JavaScript and JScript, both parties gave browsers powerful functions on a large scale. Microsoft has added many proprietary things to web technology, including VBScript, ActiveX, and Microsoft's own DHTML format, which makes many web pages unable to display properly using non-Microsoft platforms and browsers. DOM is the masterpiece brewed at that time.

DOM (Document Object Model) is the application programming interface (API) of HTML and XML. DOM will plan the entire page into a document composed of node levels.

The so-called Document Object Model is actually an internal representation of various elements in the HTML of a web page, such as headers, paragraphs, lists, styles, IDs, etc. in HTML. All elements can be passed through the DOM Come visit.

JavaScript ultimately needs to operate the Html page and turn Html into DHtml, and the DOM is required to operate the Html page. DOM simulates the Html page into an object. If JavaScript only performs some calculations, loops and other operations, but cannot operate Html, it will lose the meaning of its existence.

DOM is the model of the Html page. Each tag is treated as an object. JavaScript can programmatically control text boxes, layers and other elements in the web page by calling the properties and methods in the DOM. For example, by manipulating the DOM object of the text box, you can read and set the value in the text box.

JavaScript can change all HTML elements in the page

JavaScript can change all HTML attributes in the page

JavaScript can change all CSS styles in the page

JavaScript is able to react to all events in the page


Find HTML elements

Usually, with JavaScript, you need to manipulate HTML elements.

In order to do this, you must first find the element. There are three ways to do this:

Find the HTML element by id

Find the HTML element by tag name

Find the HTML element by class name

The simplest way to find an HTML element in the DOM is by using the element's id.

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
<head>
<body>
    <div id="div1">
        <p id="p1">
            我是第一個(gè)P</p>
        <p id="p2">
            我是第二個(gè)P</p>
    </div>
 <script>
    window.onload = function () {
            var str = document.getElementById("p1").innerHTML;
            alert(str);        //彈出    我是第一個(gè)P
        }
  </script>
</body>
</html>

Find HTML elements by tag names

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
<head>
<body>
    <div id="div1">
        <p id="p1">
            我是第一個(gè)P</p>
        <p id="p2">
            我是第二個(gè)P</p>
    </div>
    <script>
        window.onload = function () {
            var str = document.getElementsByTagName("p")[1].innerHTML;
            alert(str);        //輸出  我是第二個(gè)P,因?yàn)楂@取的是索引為1的P,索引從0開始
        }   
      </script>
</body>
</html>

##Find HTML elements by class names

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
<head>
<body>
    <div id="div1">
        <p id="p1" class="class1">
            我是第一個(gè)P</p>
        <p id="p2">
            我是第二個(gè)P</p>
    </div>
    <script>
        window.onload = function () {
            var node = document.getElementsByClassName("class1")[0];
            alert(node.innerHTML);
        }
      </script>
</body>
</html>
Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <head> <style type="text/css"> body {background-color:#eeeeee} </style> </head> <body> <h3>通過 id 查找 HTML 元素</h3> <p id = "hw">Hello world!</p> <script> x = document.getElementById("hw"); document.write('<p>id="hw"的段落的文本是:'+x.innerHTML+'</p>'); </script> <button onclick = "setCurrentTime()">將id="hw"的文字改為當(dāng)前時(shí)間</button> <script> function setCurrentTime(){ x = document.getElementById("hw"); x.innerHTML = Date() } </script> <h3>通過 標(biāo)簽名 查找 HTML 元素</h3> <div id = "mainDiv"> <p>This is a paragragph.</p> <p>This is another paragragph.</p> <p>Yes you're right! This is also paragragph.</p> </div> <script> xx = document.getElementById("mainDiv"); tagContents = xx.getElementsByTagName("p"); document.write("<p>使用Javascript查找id為mainDiv下的p標(biāo)簽的內(nèi)容</p>"); for(i=0;;i++){ var tag = tagContents[i] if(tag!=null){ document.write("<p>"+tag.innerHTML+"</p>") }else{ document.write("<p>共有"+i+"條內(nèi)容</p>") break; } } </script> <h3>修改 CSS 樣式</h3> <p> <span id = "para_1">This is a test paragraph.</span> <button onclick="changeTextColor()">改變文字顏色</button> </p> <p> <span id = "para_2">This is another paragraph. <button onclick="changeTextFont()">改變字體</button> </p> <p> <span id = "para_3">This is HELLO WORLD.</span> <button onclick="changeTextSize()">改變字號(hào)</button> </p> <p> <button onclick="changeVisibility()">顯示/隱藏</button> <span id = "para_4">示例文字</span> </p> <script> function changeTextColor(){ ele_1 = document.getElementById("para_1"); ele_1.style.color = "red"; } function changeTextFont(){ ele_2 = document.getElementById("para_2"); ele_2.style.fontFamily = "Arial"; } function changeTextSize(){ ele_3 = document.getElementById("para_3"); ele_3.style.fontSize = "larger"; } document.getElementById("para_4").style.visibility = "visible" function changeVisibility(){ ele_4 = document.getElementById("para_4"); if(ele_4.style.visibility.match("visible")){ ele_4.style.visibility = "hidden" }else{ ele_4.style.visibility = "visible" } } </script> </body> </html>
submitReset Code