DOM和BOM

BOM(Browser Object Model)是指瀏覽器對象模型,它使 JavaScript 有能力與瀏覽器進行「對話」。html

DOM (Document Object Model)是指文檔對象模型,經過它,能夠訪問HTML文檔的全部元素。瀏覽器

如下是個人示例:ide

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BOM and DOM</title>
</head>
<body>
<h1>DOM和BOM應用實例:</h1>
<p id="con">這是隱藏和顯示的效果。</p>
<form>
<input type="button" onclick="hidetext()" value="隱藏內容" />
<input type="button" onclick="showtext()" value="顯示內容" />
</form>
<p id="intro">這是DOM和BOM的查找實例:</p>
<p>這是一個例句!!!</p>
<p id="p1">這是原來的句子</p>
<p>如下例子是用id來尋找元素:</p>
<p id="demo"></p>
<p>如下例子是用標籤名稱來尋找元素:</p>
<p id="demo1"></p>
<script>
function hidetext()
        {
        var mychar = document.getElementById("con");
mychar.style.display="none";
        }
        function showtext()
        {
        var mychar = document.getElementById("con");
mychar.style.display="block";
        }
var myElement = document.getElementById("intro");
document.getElementById("demo").innerHTML =
"The text from the intro paragraph is: " + myElement.innerHTML;
var x = document.getElementsByTagName("p");
document.getElementById("demo1").innerHTML =
'The second paragraph (index 1) is: ' + x[2].innerHTML;
document.getElementById("p1").innerHTML = "本句是用js更改後的句子";
</script>
</body>
</html>
相關文章
相關標籤/搜索