Home  >  Article  >  Web Front-end  >  How to implement JavaScript to calculate a value and then embed the value into html

How to implement JavaScript to calculate a value and then embed the value into html

高洛峰
高洛峰Original
2016-12-08 14:02:451730browse

As shown below:

<!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>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn