如果某参数的列只有一个参数,那么each是失败,请看下面的例子

Java代码:  
  • <!DOCTYPE html>  
  • <html>  
  • <head>  
  •   <style>  
  •   p { margin: 8px; font-size:16px; }  
  •   .selected { color:red; }  
  •   .highlight { background:yellow; }  
  •   </style>  
  •   <script src="http://code.jquery.com/jquery-latest.js"></script>  
  • </head>  
  • <body>  
  •   Hello  
  •   
  •   and  
  •   
  •   Goodbye  
  •   
  • script配合style一起实现在script修改样式。  
  • <script>  
  •      var pp=3;  
  •      var a=[pp];  
  •      alert(a);  
  •      $.each(pp,function(i,n){  
  •       alert(i);  
  •      });  
  •  </script>  
  •   
  • </body>  
  • </html>  

  •   结果是第一次alert是3,第二次没有alert,说明pp不是数组,dom,jason等。 
    如何保证pp严格是数组呢,很简单var a=[pp];这一句就行了,下面我们把each里面的pp换成a,则结果是3,0,正确。注意这个 中括号把pp转换成了数组

    Java代码:  
  • <!DOCTYPE html>  
  • <html>  
  • <head>  
  •   <style>  
  •   p { margin: 8px; font-size:16px; }  
  •   .selected { color:red; }  
  •   .highlight { background:yellow; }  
  •   </style>  
  •   <script src="http://code.jquery.com/jquery-latest.js"></script>  
  • </head>  
  • <body>  
  •   Hello  
  •   and   
  •   Goodbye  
  •   
  • script配合style一起实现在script修改样式。  
  • <script>  
  •      var pp=3;  
  •      var a=[pp];  
  •      alert(a);  
  •      $.each(a,function(i,n){  
  •       alert(i);  
  •      });  
  •  </script>  
  •   
  • </body>  
  • </html>  
  •   总结:当参数个数小于2时,要严格保证参数列为数组,严格每个都执行each操作,则需要对参数列长度是否大于2进行分别对待。

    相关文章:

    • 2022-12-23
    • 2021-12-02
    • 2022-12-23
    • 2021-10-21
    • 2022-01-17
    • 2022-12-23
    • 2021-06-24
    • 2022-12-23
    猜你喜欢
    • 2022-12-23
    • 2023-02-15
    • 2021-12-03
    • 2022-12-23
    • 2022-12-23
    • 2022-12-23
    • 2022-01-16
    相关资源
    相似解决方案