[关闭]
@Tean 2016-12-29T08:49:55.000000Z 字数 3111 阅读 892

Javascript-2

javascript


一、输出

  1. document.write('hello');
  2. document.writeln('hello');
  3. console.log('hello');
  4. console.info('hello');

二、弹框

  1. alert('hello');
  2. confirm('hello');
  3. prompt('titile', 'default value');

三、事件

3.1 事件

  1. onclick
  2. onfocus
  3. onblur
  4. onchange
  5. ondblclick
  6. onkeydown
  7. onkeyup
  8. onkeypress
  9. onmouseover
  10. onmouseout
  11. onmousemove
  12. onmousedown
  13. onmouseup
  14. onsubmit
  15. onload
  16. onresize
  17. onscroll
  1. // 给DOM添加事件
  2. dom.onclick = function() {
  3. // TODO...
  4. };
  5. // 添加事件监听
  6. // 标准浏览器
  7. dom.addEventListener('click', function() {
  8. // TODO ...
  9. }, false);
  10. // IE
  11. dom.attachEvent('onclick', function() {
  12. // TODO ....
  13. });

3.2 事件对象

  1. dom.onclick = function(event) {
  2. // 标准浏览器:把event作为参数传入
  3. // IE: window.event
  4. // 通常兼容性写法:
  5. var event = event || window.event;
  6. };
  1. // 获取事件源
  2. if(event) {
  3. var d = event.target; // 标准浏览器
  4. } else {
  5. var d = window.event.srcElement; // IE对浏览器
  6. }
  1. // 获取鼠标位置
  2. // 浏览器
  3. event.clientX
  4. event.clientY
  5. // 页面
  6. event.pageX
  7. event.pageY
  8. // 屏幕
  9. event.screenX
  10. event.screenY
  1. // 获取
  2. event.keyCode // 键盘按键
  3. event.button // 鼠标按键
  4. // 阻止事件冒泡
  5. event.stopPropagation(); // 标准浏览器
  6. window.event.cancelBubble = true; // IE浏览器
  7. // 阻止默认行为
  8. event.preventDefault(); // 标准浏览器
  9. window.event.returnValue = false; // IE浏览器

四、DOM

4.1 节点

  1. 元素节点 [Element]
  2. 属性节点 [Attribute]
  3. 文本节点 [Text]
  4. nodeName
  5. nodeValue
  6. nodeType

4.2 属性

  1. .firstChild
  2. .lastChild
  3. .parentNode
  4. .childNodes

4.3 方法

  1. // 获取
  2. .getElementById()
  3. .getElementsByTagName()
  4. .getElementsByName()
  5. .getElementsByClassName()
  6. .querySelector()
  7. .querySelectorAll()
  8. // 创建
  9. .createElement()
  10. .createAttribute()
  11. .createTextNode()
  12. // 操作
  13. .appendChild()
  14. .removeChild()
  15. .replaceChild()
  16. .cloneNode()
  17. .insertBefore()
  18. // 属性
  19. .setAttribute('name', 'value');
  20. .getAttribute('name')
  21. // 获取元素中的内容
  22. .innerHTML
  23. .innerText
  24. // 修改图片路径
  25. img.src = 'newsrc';
  26. img.setAttribute('src', 'newsrc');
  27. // 获取常用节点
  28. document.documentElement; // html
  29. document.body; // body
  30. document.images; // 所有图片
  31. document.forms; // 所有表单
  32. document.anchors; // 所有超链接
  1. // 获取样式
  2. box.style.xxx // 操作的是内联样式
  3. box.style[xxx]
  4. box.className = 'xx'; // 改变类名
  5. // 获取box在css中的xxx样式值
  6. getComputedStyle(box).xxx // 标准
  7. box.currentStyle.xxx // IE
  1. // 尺寸
  2. .offsetLeft
  3. .offsetTop
  4. .offsetWidth
  5. .offsetHeight
  6. .clientLeft
  7. .clientTop
  8. .clientWidth
  9. .clientHeight
  10. .scrollLeft
  11. .scrollTop
  12. .scrollWidth
  13. .scrollHeight

五、BOM

  1. window

5.1 history

  1. history.back(); // 后退
  2. history.forward(); // 前进
  3. history.go()

5.2 location

  1. location.href // 可以获取或设置路径
  2. location.searh // ?后的参数
  3. location.reload() // 刷新

5.3 screen

5.4 navigator

  1. navigator.userAgent

5.5 document

  1. document.title // 设置或获取网页标题

六、对象

6.1 定义对象

  1. var obj = new Object()
  2. var obj = {};
  3. Function Person() {}
  4. var obj = new Person();
  5. // 工厂模式
  6. function factory(name, age) {
  7. var obj = new Object();
  8. obj.name = name;
  9. obj.age = age;
  10. return obj;
  11. }
  12. var p1 = factory('tom',23);

6.2 原型、原型链

  1. function Person(name, age) {
  2. this.name = name;
  3. this.age = age;
  4. }
  5. Person.prototype.eat = function() {};
  6. var obj = new Person('tom', 23);
  7. obj.__proto__

6.3 继承

  1. function Student(){}
  2. // 让学生继承Person
  3. Student.prototype = new Person();
  4. Student.prototype.constructor = Student;

七、AJAX

7.1 使用

  1. // get
  2. var xhr = new XMLHttpRequest();
  3. xhr.open('GET', 'url?name=xxx&pwd=xxx', true);
  4. xhr.send();
  5. xhr.onreadystatechange = function() {
  6. if(xhr.readyState == 4 && xhr.status == 200) {
  7. var data = xhr.responseText;
  8. }
  9. };
  1. // post
  2. var xhr = new XMLHttpRequest();
  3. xhr.open('POST', 'url', true);
  4. // post请求一定要加这个,否则服务器拿不到数据
  5. xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  6. xhr.send('name=xxx&pwd=xxx');
  7. xhr.onreadystatechange = function() {
  8. if(xhr.readyState == 4 && xhr.status == 200) {
  9. var data = xhr.responseText;
  10. }
  11. };

7.3 跨域

  1. 1. document.domain
  2. 2. jsonp
  3. 3. cors
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注