Prototype – $R() 方法

返回到:Prototype – 实用方法

$R() 函数只是编写 new ObjectRange(lowerBound, upperBound, excludeBounds) 的简写。

语法

$R(start, end[, exclusive = false]);

这里,start是范围的起始元素,end是范围的最后一个元素。如果exclusive flag设置为false,那么它会包含结束元素,否则不会包含在范围内。

返回值

范围对象。

例子

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function ShowValues() {
            var range = $R(10, 20, false);
            
            range.each(function(value, index) {
               alert(value);
            });
         }
      </script>
   </head>
   
   <body>
      <p>Click "Show Value" button to see the result</p>
      
      <form>
         <input type = "button" value = "Show Value" onclick = "ShowValues();"/>
      </form>
      
   </body>
</html>

更多例子

以下语句返回true

$R(0, 10).include(10);

以下语句返回字符串“0、1、2、3、4、5”

$A($R(0, 5)).join(', ');

以下语句返回一个字符串“aa, ab, ac, ad, ae, af, ag, ah”

$A($R('aa', 'ah')).join(', ');

以下声明返回false

$R(0, 10, true).include(10);

对于值 = 0 到 9,以下语句将被调用 10 次

$R(0, 10, true).each(function(value) {
   // invoked 10 times for value = 0 to 9
});

返回到:Prototype – 实用方法

作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8945.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年2月7日
下一篇 2023年2月7日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注