This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 查找所有含有“fileDropTarget” 类的元素 | |
// 并注册DnD 事件处理程序使它们能响应文件的拖放 | |
// 当文件放下时,上传它们到data-uploadto 属性指定的URL | |
whenReady(function(){ | |
var elts = document.getElementsByTagName("fileDropTarget"); | |
for (var i = 0; i < elts.length; i++) { | |
var target = elts[i]; | |
var url = target.getAttribute("data-uploadto"); | |
if(!url) continue; | |
createFileUploadDropTarget(target, url) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 编码对象的属性 | |
* 如果它们是来自HTML 表单的名/值对,使用application/x-www-form-urlencoded 格式 | |
*/ | |
function encodeFormData(data){ | |
if(!data) return ""; // 一直返回字符串 | |
var pairs = []; // 为了保存名=值对 | |
for(var name in data){ // 为每个名字 | |
if(!data.hasOwnProperty(name)) continue; // 跳过继承属性 | |
if(typeof data[nam] === "function") continue; // 跳过方法 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 不要把响应作为XML 文档处理 | |
request.overrideMimeType("text/palin; charset=utf-8"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 发出一个HTTP GET请求以获得指定URL 的内容 | |
// 当响应成功到达,验证它是否是纯文本 | |
// 如果是,把它传递给指定回调函数 | |
function getText(url, callback){ | |
var request = new XMLHttpRequest(); // 创建新请求 | |
request.open("GET", url); // 指定待获取的URL | |
request.onreadystatechange = function(){ // 定义事件处理程序 | |
// 如果请求完成, 则它是成功的 | |
if(request.readyState === 4 && request.status === 200){ | |
var type = request.getResponseHeader("Content-Type"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Keymap.js 绑定键盘事件和处理程序函数 | |
* | |
* 这个模块定义一个Keymap 类 | |
* 这个类的实例表示按键标识符(下面有定义)到处理程序函数的映射 | |
* Keymap 能配置到HTML 元素上以处理keydown 事件 | |
* 当此类事件发生时,Keymap 会使用它的映射来调用合适的处理程序 | |
* | |
* 当创建Keymap 时, | |
* 能传入一个JavaScript 对象,它表示Keymap 绑定的初始设置 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function forceToUpperCase(element){ | |
if(typeof element === "string") element = document.getElementById(element); | |
element.oninput = upcase; | |
element.onpropertychange = upcaseOnPropertyChange; | |
// 简单案例:用于input 事件的处理程序 | |
function upcase(event) { this.value = this.value.toUpperCase(); } | |
// 疑难案例:用于propertychange 事件的处理程序 | |
function upcaseOnPropertyChange(event){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* InputFilter.js 不唐突地过滤<input> 元素的键盘输入 | |
* | |
* 这个模块查找文档中拥有“data-allowed-chars” 属性的所有<input type="text"> 元素 | |
* 它为所有这类元素都注册keypress、textInput 和textinput 事件处理程序, | |
* 来限制用户只能输入出现在许可属性值中的字符 | |
* 如果<input> 元素也有一个“data-messageid” 属性, | |
* 那么认为这个值是另一个文档元素的id | |
* 如果用户输入了不允许的字符,那么会显示消息元素 | |
* 如果用户输入了允许的字符,那么会隐藏消息元素 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 传递函数给whenReady(),当文档解析完毕且为操作准备就绪 时, | |
* 函数将作为文档对象的方法调用 | |
* DOMContentLoaded、readystatechange 或load 事件发生时会触发注册函数 | |
* 一旦文档准备就绪,所有函数都将被调用,任何传递给whenReady 的函数都将立即调用 | |
*/ | |
var whenReady = (function(){ // 这个函数返回whenReady() 函数 | |
var funcs = []; // 当获得事件时,要运行的函数 | |
var ready = false; // 当触发事件处理程序时,切换到true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cancelHandler(event){ | |
var event = event || window.event; // 用于IE | |
/* 这里是处理事件的代码 */ | |
// 现在取消事件相关的默认行为 | |
if(event.preventDefault) event.preventDefault(); // 标准技术 | |
if(event.returnValue) event.returnValue = false; // IE | |
return false; // 用于处理使用对象属性注册的处理程序 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 在指定的事件目标上注册用于处理指定类型事件的指定处理程序函数 | |
* 确保处理程序一直作为事件目标的方法调用 | |
*/ | |
function addEvent(target, type, handler){ | |
if(target.addEventListener) | |
target.addEventListener(type, handler, false) | |
else | |
target.attachEvent("on" + type, function(event){ | |
// 把处理程序作为事件目标的方法调用 |