一、什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作。其中, […]
javascript
三级(多级)联动自动赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>三级联动</title> </head> <body> <select name="one" id="one"> </select> <select name="two" id="two"> </select> <select name="three" id="three"> </select> <script src="./jquery-1.11.1.js"></script> <script> function clear( obj ) { obj.length = 0; } function make( obj, data ) { for( var i in data ) { obj.options.add( new Option( data[i], i ) ); } } function ajax( url, obj, val ) { var dfd = $.Deferred(); obj = $('#'+obj).get(0); $.ajax(url,{cache:false}).done(function(rss) { clear( obj ); make( obj, rss ); obj.value = val; dfd.resolve(); }); return dfd.promise(); } var arr = [ {"url":"data_1.json","obj":"one","val":"021"}, {"url":"data_2.json","obj":"two","val":"2"}, {"url":"data_3.json","obj":"three","val":"3"} ]; function r( arr ) { var a = arr.shift(); ajax( a.url, a.obj, a.val ).done(function() { if( arr.length ) r( arr ); }); } r(arr); // 方法二 var onv = '021', twv = '2', thv = '3'; // $.ajax( "data_1.json" ).then(function( rss ) { // clear( one ); // make( one, rss ); // one.value = onv; // return $.ajax( "data_2.json" ); // }).then(function( rss ) { // clear( two ); // make( two, rss ); // two.value = twv; // return $.ajax( "data_3.json" ); // }).then(function( rss ) { // clear( three ); // make( three, rss ); // three.value = thv; // }); // 方法三 // $.when( // $.ajax( "data_1.json" ), // $.ajax( "data_2.json" ), // $.ajax( "data_3.json" ) // ).then( function( a, b, c ) { // clear( one ); clear( two ); clear( three ); // make( one, a[0] ); make( two, b[0] ); make( three, c[0] ); // one.value = onv; two.value = twv; three.value = thv; // } ); </script> </body> </html> |
测试连接 参考: http://api.jq […]
backbone.js(六)后台交互
backbone.js(五)Router
一、基本用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * 路由器 Router */ var NoteRouter = Backbone.Router.extend({ // 定义路由规则 routes: { notes: 'index', }, // 具体实现操作 index: function() { jQuery( '#note_list' ).html( noteCollectionView.el ); console.log( '笔记列表' ); } }); // 路由 var noteRouter = new NoteRouter(); // 开始监视hashchange事件 Backbone.history.start(); |
假设域名为www.notes. […]
backbone.js(四)Collection
backbone.js(三)View
一、基本方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/** * View 试图 */ var NoteView = Backbone.View.extend({ // 标签名 tagName : 'li', // 类 className: 'item', // 属性 attributes: { 'data-role': 'list' } }); var noteView = new NoteView(); console.log( noteView.el ); // <li data-role="list" class="item"></li> |
二、视图绑定模型 [cray […]
backbone.js(二)Model
一、基本方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/** * 模型 */ var Note = Backbone.Model.extend({ // 定义默认属性 defaults: { title: 'one note', create_at: new Date() }, // 初始化,执行方法 initialize: function() { console.log( '新创建了一条笔记:' + this.get( 'title' ) ); } }); // 创建note1实例 var note1 = new Note( { title: 'Hello', author: 'cc' } ); // 获取属性 console.log( note1.get( 'title' ) ); // Hello console.log( note1.get( 'notitle' ) ); // undefined // 获取所有属性 console.log( note1.toJSON() ); // Object {title: "Hello", author: "cc"} console.log( note1.attributes ); // Object {title: "Hello", author: "cc"} // 是否存在某个key值 console.log( note1.has( 'title' ) ); // true console.log( note1.has( 'notitle' ) ); // false // 删除属性 note1.unset( 'title' ); // 设置属性 note1.set( 'hello', 'world' ); note1.set( { content: 'nihao, 第一个笔记', title: 'Hello' } ); console.log( note1.toJSON() ); // Object {author: "cc", hello: "world", content: "nihao, 第一个笔记", title: "Hello"} // 清空所有属性 note1.clear(); |
二、事件 当使用set方法改 […]
backbone.js(一)初识
WebSocket(四) —— 传输图片
前文解析数据帧中图,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued ... | +---------------------------------------------------------------+ |
现在再看 […]