這篇文章主要介紹了JS實現改變HTML上文字顏色和內容的方法,涉及JS數學運算與頁面元素動態操作相關技巧,需要的朋友可以參考下
本文實例講述了JS實現改變HTML上文字顏色和內容的方法。分享給大家參考,如下:
1. JavaScript
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Day 1 </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script> // to change the color of an object. function changeColor(){ x=document.getElementById("Id1"); x.style.color="Red"; } // to change the content of an object. function changeContent(){ y=document.getElementById("Id2"); y.innerHTML="Hi, you have changed the contents using Java Script successfully!"; } </script> </HEAD> <BODY bgcolor="AntiqueWhite"> <h1 ><center>Welcome Page</center></h1> <p id="Id1">It is test 1.</p> <p id="Id2">It is test 2.</p> <button type="button" onclick ="changeColor()"> Change color of test 1 </button><br/><br/> <button type="button" onclick ="changeContent()"> Change content of test 2 </button> </BODY> </HTML>
2. Day 1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Day 1 </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script> // to change the color of an object. function changeColor(){ x=document.getElementById("Id1"); x.style.color="Red"; } // to change the content of an object. function changeContent(){ y=document.getElementById("Id2"); y.innerHTML="Hi, you have changed the contents using Java Script successfully!"; } </script> </HEAD> <BODY bgcolor="AntiqueWhite"> <h1 ><center>Welcome Page</center></h1> <p id="Id1">It is test 1.</p> <p id="Id2">It is test 2.</p> <button type="button" onclick ="changeColor()"> Change color of test 1 </button><br/><br/> <button type="button" onclick ="changeContent()"> Change content of test 2 </button> </BODY> </HTML>
3. 運行效果截圖:
#相關推薦:
#
以上是JS實作改變HTML上文字顏色和內容的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!