Home > Web Front-end > JS Tutorial > 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

高洛峰
Release: 2016-12-08 14:02:45
Original
1760 people have browsed it

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>
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template