目录

indexOf('value')

描述 (Description)

KnockoutJS Observable indexOf('value')方法返回所提供参数的第一次出现的索引。 如果未找到匹配元素,则此函数将返回-1。

语法 (Syntax)

arrayName.indexOf('value')

参数 (Parameters)

接受1参数,其索引将被返回。

例子 (Example)

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray indexOf method</title>
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js"
         type = "text/javascript"></script>
   </head>
   <body>
      <p>Example to demonstrate indexOf() method.</p>
      <p>Index of Employee 'Jordan':
         <span data-bind = "text: empArray().indexOf('Jordan')"></span>
      </p>
      <p>Array of employees: <span data-bind = "text: empArray()" ></span></p>
      <script>
         function EmployeeModel() {
            this.empName = ko.observable("");
            this.chosenItem = ko.observableArray("");
            this.empArray = ko.observableArray(['Scott','James','Jordan','Lee',
               'RoseMary','Kathie']);
         }
         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>
   </body>
</html>

输出 (Output)

让我们执行以下步骤来查看上述代码的工作原理 -

  • 将上面的代码保存在array-indexof.htm文件中。

  • 在浏览器中打开此HTML文件。

新页面打开
↑回到顶部↑
WIKI教程 @2018