js相关的常用事件
加载事件
-
鼠标悬停事件
onmouseover():鼠标移入事件
- onmouseout():鼠标移出事件
- onblur():元素失去焦点时
-
点击事件
onclick():点击后发生的事件,绑定在标签里
- ondblclick():双击某个对象时调用的事假
-
键盘事件
onkeydown():某个键被按下后(不松手)
- onkeypress():某个键被按下并松开
-
域变化事件
onchange():域的内容被改变时
this关键字
函数内部使用this:代表当前操作的元素对象
setAttribute()设置属性
setAttribute(name,value):给当前元素设置属性
document文档对象-操作DOM
获取元素
- getElementById():返回对拥有指定id的第一个对象的引用
- getElementsByName():返回带有指定名称的对象集合
getElementsByTagName():返回带有指定标签名的对象集合
创建元素
如何创建
document.createElement():创建指定名称的元素 - document.createElement(‘div’); - 返回一个element对象
document.createTextNode():创建一个标签内的文本内容 - document.createTextNode(‘我是div’);
如何实现
element对象表示HTML文档中的元素(HTML称为标签)
element.appendChild():向元素添加新的子节点,作为最后一个子节点
- element.firstChild():返回元素的首个子节点
- element.getAttribute():返回元素节点的指定属性值
- element.innerHTML:设置或返回元素的内容
- element.insertBefore():在指定的已有的子节点之前插入新节点
- element.lastChild():返回元素的最后一个子元素
- element.removeChild():从元素中移除子节点
element.setAttribute():把指定属性设置或更改为指定值
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script>window.onload = function(){init()}function init(){let box = document.querySelector('.box');let table = document.createElement('table');let thead = document.createElement('thead');let tr = document.createElement('tr');let th = document.createElement('th');let th2 = document.createElement('th');let th3 = document.createElement('th');let text = document.createTextNode('id');th.appendChild(text);text = document.createTextNode('name');th2.appendChild(text);text = document.createTextNode('age');th3.appendChild(text);tr.appendChild(th);tr.appendChild(th2);tr.appendChild(th3);thead.appendChild(tr);table.appendChild(thead);box.appendChild(table);// box.lastElementChild(table);}</script></head><body><div class="box"></div></body></html>
案例-实现级联选框


<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><div><select id="province"></select><select id="city"></select><select id="county"></select></div><script type="text/javascript" src="./js/data.js"></script><script>window.onload = function(){init()}function init(){//es6新特性,变量名名必须与对象中属性名一致(获取data对象属性的值,并赋给变量)let {province_list,city_list,county_list} = data;//disabled selected实现该标签只显示不能选中let options = '<option disabled selected>请选择</option>';for(let province_key in province_list){//``模板样式,通过${}替换值options += `<option value="${province_key}">${province_list[province_key]}</option>`}let province = document.getElementById('province');let city = document.getElementById('city');let county = document.getElementById('county');province.innerHTML = options;province.onchange = function(){let pre = this.value.substr(0,2);let options_city = '';for(let city_key in city_list){if(city_key.substr(0,2) == pre){options_city += `<option value="${city_key}">${city_list[city_key]}</option>`}}city.innerHTML = options_city;let pre2 = city.firstChild.value.substr(0,4);let options_city2 = '';for(let city2_key in county_list){if(city2_key.substr(0,4) == pre2){options_city2 += `<option value="${city2_key}">${county_list[city2_key]}</option>`}}county.innerHTML = options_city2;}city.onchange = function(){let pre2 = this.value.substr(0,4);let options_city2 = '';for(let city2_key in county_list){if(city2_key.substr(0,4) == pre2){options_city2 += `<option value="${city2_key}">${county_list[city2_key]}</option>`}}county.innerHTML = options_city2;}}</script></body></html>
