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>