怎么获取javascript的值返回值

JavaScript014

怎么获取javascript的值返回值,第1张

函数具有返回值,直接调用函数后赋值给变量就可以获取到。

1、定义一个函数

function test(){//定一个test函数 return 1//返回值为1}

2、获取返回值var rtn = test()//调用test函数,并把返回值赋值给rtn

<!DOCTYPE html>

<html>

  <head>

      <meta charset="utf-8">

      <title>标题</title>

      <style type="text/css"></style>

      <script type="text/javascript">

          window.onload=function(){

              var oT1=document.getElementById('text1')

              var oT2=document.getElementById('text2')

              var oB=document.getElementById('btn')

              oB.onclick=function(){

                  var a=oT1.value

                  var b=oT2.value

                  alert(getValue(a,b))

              }

              function getValue(a,b){

                  var sum=parseInt(a)+parseInt(b)

                  return sum

              }

          }

      </script>

  </head>

  <body>

      <input type="text" id="text1">

      <input type="text" id="text2">

      <input type="button" value="获取" id="btn">

  </body>

</html>