添加删除记录-删除
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><script type="text/javascript">window.onload = function(){//点击链接以后删除信息var allA = document.getElementsByTagName("a");for(var i = 0;i<allA.length;i++){allA[i].onclick = function(){var tr = this.parentNode.parentNode;var name = tr.children[0].innerHTML;var flag = confirm("sure?"+name);if (flag){tr.parentNode.removeChild(pr);}return false; //点击后不要跳转};}};</script><title>测试</title></head><body></body></html>
for循环会在加载完后立即执行
响应函数会在点击时执行
响应函数执行时,for早已执行完毕
添加删除记录-添加
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><script type="text/javascript">window.onload = function(){var addButton = document.getElementById("button");addButton.onclick = function(){var name = document.getElementById("addName").value;var email = document.getElementById("addEmail").value;var salary = document.getElementById("addSalary").value;var tr = document.createElement("tr");var a = document.createElement("a");var nameTd = document.createElement("td");var emailTd = document.createElement("td");var salaryTd = document.createElement("td");var aTd = document.createElement("td");var nameText = document.createTextNode(name);var emailText = document.createTextNode(email);var salaryText = document.createTextNode(salary);var deleteText = document.createTextNode("Delete");nameTd.appendChild(nameText);emailTd.appendChild(emailText);salaryTd.appendChild(salaryText);a.appendChild(deleteText);tr.appendChild(nameTd);tr.appendChild(emailTd);tr.appendChild(salaryTd);tr.appendChild(aTd);a.href ="javascript:;"a.onclick = dleA;var tabel = document.getElementById("table");table = appendchild(tr);};</script><title>测试</title></head><body></body></html>
添加删除记录-修改
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><script type="text/javascript">window.onload = function(){var name = document.getElementById("addName").value;var email = document.getElementById("addEmail").value;var salary = document.getElementById("addSalary").value;var addButton = document.getElementById("button");addButton.onclick = function(){var tr = document.createElement("tr");tr.innerHTMl = "<td>"name+"</td>"+"<td>"+email+"</td>"+"<td>"+salary+"</td>"+"<td><a href = "javascript:;">delete</a></td>";var a = tr.getElenemtsByTagName("a")[0];a.onclick = delA;var tabel = document.getElementById("table");table = appendchild(tr);};</script><title>测试</title></head><body></body></html>
操作内联样式
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#box{width:200px;height:200px;background-color:green !important;}</style><script type="text/javascript">window.onload = function(){//点击按钮更改大小var box = document.getElementById("box");var btn = document.getElementById("btn");btn.onclick = function(){box.style.width = "300px";box.style.backgroundColor="yello";//该方法修改的是内联样式,有较高的级别};//读取元素的样式var btn = document.getElementById("btn");btn.onclick = function(){alert(box.style.width);//只能读取内联样式};};</script><title>测试</title></head><body><button id="btn">Button</button><div id="box"></div></body></html>
获取元素的样式
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#box{width:200px;height:200px;background-color:green !important;}</style><script type="text/javascript">window.onload = function(){//点击按钮更改大小var box = document.getElementById("box");var btn = document.getElementById("btn");btn.onclick = function(){alert(box.currentStyle.width);//当前元素正在显示的样式 仅ie支持var obj = getComputedStyle(box,null).width;//返回对象 如果没有设置,会实时大小};};//定义函数来获取指定元素当前样式//兼容所有function style(obj,name){if(window.getComputedStyle){ //将变量变为属性 找不到=》没有定义return getComputedStyle(box,null)[name];}else{return box.currentStyle.width;}};</script><title>测试</title></head><body><button id="btn">Button</button><div id="box"></div></body></html>
其他样式相关属性
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#box{width:200px;height:200px;padding:10px;background-color:green !important;}#box2{width:150px;height:300px;background-color:yello;}</style>//以下只读不能改<script type="text/javascript">window.onload = function(){//点击按钮更改大小var box = document.getElementById("box");var btn = document.getElementById("btn");btn.onclick = function(){alert(box.clientWidth);//不带px,返回数字,包括内容区和内边距alert(box.offsetWidth);//获取整个大小 包括多一个边框var op = box.offsetParent;//会获取到离当前元素最近开启了定位的祖先元素//如果都没开启,则是bodyalert(box.offsetleft);var box = document.getElementById("box");alert(box.clientHeigth);//可见的高度alert(box.scrollHeight);//滚动的高度alert(box.scrollLeft);//当满足scrollHeight-scrollTop=clientHeigth说明滚动到底};};//阅读到底才能注册window.onload = function(){var info = document.getElementById("info");//获取info的p元素var inputs = document.getElementByTagName("input");info.onscroll = function(){ //绑定滚动事件if(scrollHeight-scrollTop=clientHeigth){inputs[0].disable = false;inputs[1].disable = false;//设置是否禁用}};};</script><title>测试</title></head><body><button id="btn">Button</button><div id="box"><div id= "box2"></box></div><input type="checkbox" disable = "disable" />我已经阅读<input type="submit" value = "注册" disable = "disable" /></body></html>
事件对象
事件对象:
当事件的响应函数触发时,浏览器每次会将一个事件对象作为实参传递进影响函数】
在事件对象中封装当前事件一切信息 比如 鼠标坐标 比如键盘输入
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#areaDiv,#showMsg{width:200px;height:200px;}</style><script type="text/javascript">window.onload = function(){var areaDiv = document.getElementById("areaDiv");var showMsg = document.getElementById("showMsg");areaDiv.onmousemove = function(event){ //鼠标在元素内移动触发 定一个形参,默认传入事件对象event = event || window.event;var x = (window).event.clientX;//通过窗口来算坐标的var y = (window).event.clientY;shouMsg.innerHTMl = "x="+x+"y="+y;};};</script><title>测试</title></head><body><div id="areaDiv"></div><div id="showMsg"></div></body></html>
div跟随鼠标移动
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#box{width:200px;height:200px;position:absolute;}</style><script type="text/javascript">window.onload = function(){var box = document.getElementById("box");document.onmousemove = function(event){event = event || window.event;var x = (window).event.pageX;//通过页面来算的var y = (window).event.pageY;box.style.left = left + "px";box.style.top = top + "px";};};</script><title>测试</title></head><body><div id= "box"></box></body></html>
事件的冒泡
事件的向上传导,当后代的元素事件被触发时,其祖先元素的相同事件也会被触发
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#box{width:200px;height:200px;}#s1{background-color:yello;}</style><script type="text/javascript">window.onload = function(){var s1 = document.getElementById("s1");s1.onclick = function(event){alert("1");event.cancelBubble = true;//取消冒泡};var box = document.getElementById("box");box.onclick = function(){};var body = document.getElementById("body");body.onclick = function(){};};</script><title>测试</title></head><body><div id= "box">box<span id="s1">span</span></box></body></html>
事件的委派
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#box{width:200px;height:200px;}#s1{background-color:yello;}</style><script type="text/javascript">window.onload = function(){var btn = document.getElementById("btn");var ul = document.getElementById("ul");btn.onclick = function(){var li = document.creatElement("li");li.innerHTML = "<a href='Javascript:;'>超链接</a>";ul.appendChild(li);};allA = document.getElementsByTagName("a");for(var i = 0;i<allA.length;i++){allA[i].onclick = function(){};//只能给原来的ul.onclick = function(event){if(event.target.className == "link"{alert(1);}//事件的委派,相当于利用了冒泡};};</script><title>测试</title></head><body>...</body></html>
事件的绑定
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><style type="text/css">#box{width:200px;height:200px;}#s1{background-color:yello;}</style><script type="text/javascript">window.onload = function(){var btn = document.getElementById("btn");btn.onclick = function(){alert(1);//始终只能是一个};//可以同时绑定多个响应元素btn.addEventListener("click",function(){alert(1);},false);btn.addEventListener("click",function(){alert(2);},false);//兼容所有//正常的this为button//ie为windowfunction bind(obj,eventStr,callback){if(obj.addEventListener){obj.addEventListener(eventStr,callback,false);}else{obj.attachEvent("on"+eventStr,function(){callback.call(obj)});//匿名函数中调用回调函数来this为};};</script><title>测试</title></head><body>...</body></html>
