给P增加一个属性,通过属性来判断当前状态
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>display</title> <script type="text/javascript"> function changeText() { var mychar = document.getElementById("con"); if(mychar.getAttribute('show') == "yes"){ mychar.setAttribute('show', "no"); mychar.style.display = 'none' }else{ mychar.setAttribute('show', "yes") mychar.style.display = 'block' } } </script> </head> <body> <h1>JavaScript</h1> <p id="con" show = "yes">做为一个Web开发师来说,如果你想提供漂亮的网页、令用户满意的上网体验,JavaScript是必不可少的工具。</p> <form> <input type="button" onclick="changeText()" value="改变内容" /> </form> </body> </html>