- KnockoutJS 教程
- KnockoutJS - 首頁
- KnockoutJS - 概覽
- KnockoutJS - 環境設定
- KnockoutJS - 應用程式
- KnockoutJS - MVVM 框架
- KnockoutJS - 可觀測
- 計算可觀測
- KnockoutJS - 宣告繫結
- KnockoutJS - 依賴項跟蹤
- KnockoutJS - 模板化
- KnockoutJS - 元件
- KnockoutJS 資源
- KnockoutJS - 快速指南
- KnockoutJS - 資源
- KnockoutJS - 討論
KnockoutJS - shift() 方法
說明
KnockoutJS 可觀察 shift() 方法從陣列中移除第一個元素並返回它。
語法
arrayName.shift()
引數
不接受任何引數。
示例
<!DOCTYPE html>
<head>
<title>KnockoutJS ObservableArray shift 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 shift() method.</p>
<button data-bind = "click: shiftEmp">Remove First Emp</button>
<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']);
this.shiftEmp = function() {
this.empArray.shift(); //remove first item
}
}
var em = new EmployeeModel();
ko.applyBindings(em);
</script>
</body>
</html>
輸出
讓我們執行以下步驟來了解以上程式碼如何執行 -
將以上程式碼另存為 array-shift.htm 檔案。
在瀏覽器中開啟此 HTML 檔案。
單擊“移除第一個員工”按鈕,觀察第一個元素被移除。
《knockoutjs_observables.htm》
廣告