- BackboneJS 教程
- BackboneJS - 主頁
- BackboneJS - 概述
- BackboneJS - 環境設定
- BackboneJS - 應用
- BackboneJS - 事件
- BackboneJS - 模組
- BackboneJS - 集合
- BackboneJS - 路由器
- BackboneJS - 歷史
- BackboneJS - 同步
- BackboneJS - 檢視
- BackboneJS - 實用工具
- BackboneJS 的有用資源
- BackboneJS - 快速指南
- BackboneJS - 資源
- BackboneJS - 討論
BackboneJS-檢視 $(jQuery)
說明
如果頁面包含 jQuery,每個檢視都具有一個 $ 函式,它在檢視元素內範圍執行查詢。如果你使用此分範圍的 jQuery 函式,你不必將模組 ID 用作查詢的一部分來提取列表中的特定元素,而可以更多地依賴 HTML 類屬性。這相當於執行:view.$el.find(selector)
語法
view.$(selector) |
引數
- 選擇器:它使用不同型別的選擇器,比如 id 或類。
示例
<!DOCTYPE html>
<head>
<title>View Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
</head>
<body>
<div id="myVal">
<button id="button" data-test="">Click Here</button>
</div>
<span id="myLog"></span>
<script type="text/javascript">
//The variable contains id selector as 'mydata'
var myLog = $('#mydata');
//The variable 'data' is used to display the values
var data = function(val) {
document.write(val);
};
//'ViewDemo' is a name of the view class
var ViewDemo = Backbone.View.extend({
//When click event occurs it activates the defined functions 'myFunc1' and 'myFunc2'
events: {
'click [data-test]' : 'myFunc1',
'click *[data-test]': 'myFunc2',
},
//'el' uses '#myVal' as the view reference
el: $('#myVal'),
//When user clicks the button, it refers to the button defined within the 'div' tag and
//it will display the below statements
myFunc1: function () {
data('Hello...');
},
myFunc2: function () {
data('Welcome to Tutorialspoint...');
}
});
//'myview' is an instance of the 'ViewDemo' class
var myview = new ViewDemo();
</script>
</body>
</html>
輸出
讓我們執行下列步驟,瞭解以上程式碼如何執行
將以上程式碼儲存在 **dollar-jquery.htm** 檔案中
在瀏覽器中開啟此 HTML 檔案。
廣告