JSパラメータ転送ロジック

:より転載https://www.cnblogs.com/daixiaotian/p/6423691.html

全体のhtmlでの答えは、簡単な要約は、ときにのみバックパスパラメータ未定義に複数のパラメータを受け入れることができます。

<html>
  <head>
    <title>
      函数调用测试,参数个数和函数声明不一样多
    </title>
    
    <script language="JavaScript">
      function  needTwoPara(p1,p2){
                var a=arguments;
                var result='我是个需要2个参数的函数\n'
                                     +'您输入的参数的个数为:'+a.length+'\n'
                for(var i=0, len = a.length; i < len; i++){
                        result=result+'第'+(i+1)+'个参数为:'+a[i]+'\n'
                    }
                result+='以上是用arguments来获得参数\n';
                result+='下面用变量来获得参数:\n';
                result+='p1:'+p1+'\n';
                result+='p2:'+p2+'\n';
            alert(result);
        }
     </script>
    </head>
    <body>
       <form>
          <input type="button" value="测试1--传递1个参数"
            onClick="JavaScript: needTwoPara('smallerpig');">
       </form>
       <form>
          <input type="button" value="测试2--传递2个参数"
            onClick="JavaScript: needTwoPara('smallerpig','小小猪');">
       </form>
       <form>
          <input type="button" value="测试3--传递3个参数"
            onClick="JavaScript: needTwoPara('smallerpig','小小猪','生命不息');">
       </form>
       <form>
          <input type="button" value="测试4--传递4个参数"
            onClick="JavaScript: needTwoPara('smallerpig','小小猪','生命不息','学习不止');">
       </form>
    
    </body>
</html>

 

おすすめ

転載: blog.csdn.net/i929479824/article/details/91419071