@Tean
2016-12-29T08:49:55.000000Z
字数 3111
阅读 892
javascript
document.write('hello');document.writeln('hello');console.log('hello');console.info('hello');
alert('hello');confirm('hello');prompt('titile', 'default value');
onclickonfocusonbluronchangeondblclickonkeydownonkeyuponkeypressonmouseoveronmouseoutonmousemoveonmousedownonmouseuponsubmitonloadonresizeonscroll
// 给DOM添加事件dom.onclick = function() {// TODO...};// 添加事件监听// 标准浏览器dom.addEventListener('click', function() {// TODO ...}, false);// IEdom.attachEvent('onclick', function() {// TODO ....});
dom.onclick = function(event) {// 标准浏览器:把event作为参数传入// IE: window.event// 通常兼容性写法:var event = event || window.event;};
// 获取事件源if(event) {var d = event.target; // 标准浏览器} else {var d = window.event.srcElement; // IE对浏览器}
// 获取鼠标位置// 浏览器event.clientXevent.clientY// 页面event.pageXevent.pageY// 屏幕event.screenXevent.screenY
// 获取event.keyCode // 键盘按键event.button // 鼠标按键// 阻止事件冒泡event.stopPropagation(); // 标准浏览器window.event.cancelBubble = true; // IE浏览器// 阻止默认行为event.preventDefault(); // 标准浏览器window.event.returnValue = false; // IE浏览器
元素节点 [Element]属性节点 [Attribute]文本节点 [Text]nodeNamenodeValuenodeType
.firstChild.lastChild.parentNode.childNodes
// 获取.getElementById().getElementsByTagName().getElementsByName().getElementsByClassName().querySelector().querySelectorAll()// 创建.createElement().createAttribute().createTextNode()// 操作.appendChild().removeChild().replaceChild().cloneNode().insertBefore()// 属性.setAttribute('name', 'value');.getAttribute('name')// 获取元素中的内容.innerHTML.innerText// 修改图片路径img.src = 'newsrc';img.setAttribute('src', 'newsrc');// 获取常用节点document.documentElement; // htmldocument.body; // bodydocument.images; // 所有图片document.forms; // 所有表单document.anchors; // 所有超链接
// 获取样式box.style.xxx // 操作的是内联样式box.style[xxx]box.className = 'xx'; // 改变类名// 获取box在css中的xxx样式值getComputedStyle(box).xxx // 标准box.currentStyle.xxx // IE
// 尺寸.offsetLeft.offsetTop.offsetWidth.offsetHeight.clientLeft.clientTop.clientWidth.clientHeight.scrollLeft.scrollTop.scrollWidth.scrollHeight
window
history.back(); // 后退history.forward(); // 前进history.go()
location.href // 可以获取或设置路径location.searh // ?后的参数location.reload() // 刷新
navigator.userAgent
document.title // 设置或获取网页标题
var obj = new Object()var obj = {};Function Person() {}var obj = new Person();// 工厂模式function factory(name, age) {var obj = new Object();obj.name = name;obj.age = age;return obj;}var p1 = factory('tom',23);
function Person(name, age) {this.name = name;this.age = age;}Person.prototype.eat = function() {};var obj = new Person('tom', 23);obj.__proto__
function Student(){}// 让学生继承PersonStudent.prototype = new Person();Student.prototype.constructor = Student;
// getvar xhr = new XMLHttpRequest();xhr.open('GET', 'url?name=xxx&pwd=xxx', true);xhr.send();xhr.onreadystatechange = function() {if(xhr.readyState == 4 && xhr.status == 200) {var data = xhr.responseText;}};
// postvar xhr = new XMLHttpRequest();xhr.open('POST', 'url', true);// post请求一定要加这个,否则服务器拿不到数据xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");xhr.send('name=xxx&pwd=xxx');xhr.onreadystatechange = function() {if(xhr.readyState == 4 && xhr.status == 200) {var data = xhr.responseText;}};
1. document.domain2. jsonp3. cors