如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript计算值</title> </head> <body> <p>假设y=5,计算x=y+2,并显示结果.</p> <button type="button" onclick="myFunction()">点击这里</button> <p id="pid"></p> <script> function myFunction() { var y=5; var x=y+2; var demoP=document.getElementById("pid"); demoP.innerHTML="x="+x; } </script> </body> </html>