JavaScript這門語言在必定程度上讓咱們html之間耦合度下降了,爲何這樣說呢?JavaScript語言同樣能夠能夠隨意寫入html頁面一些東西,好比:JavaScript的DOM能夠改變html的輸出、內容、屬性,下面經過幾個例子加以說明。html
語法:spa
<script> document.write(); </script>
例子:(輸出時間)code
<!DOCTYPE html> <html> <body> <script> document.write("zhoulitong"); </script> </body> </html>
語法:htm
document.getElementById(id).innerHTML=new HTML
例子:(改變標題內容)blog
<!DOCTYPE html> <html> <body> <p id="p1">Hello JavaScript!</p> <script> document.getElementById("p1").innerHTML="YES!"; </script> <p>寶寶剛纔的內容被殘忍的修改了···</p> </body> </html>
語法:圖片
document.getElementById(id).attribute=new value
例子:(點擊按鈕改變圖片屬性)ip
<span style="font-family:Microsoft YaHei;font-size:18px;"><!DOCTYPE html> <html> <body> <img id="img" src="/i/shanghai_lupu_bridge.jpg" /> <button type="button" onclick="myfunction()" >點我 </button> <script> function myfunction() { document.getElementById("img").src="/i/eg_tulip.jpg"; } </script> </body> </html></span>