标签:
%应用实例一:<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>无标题文档</title><script>/*var i = 0;i++;if( i === 5 ){i = 0;}上下两者一样i%=5;巧妙的吧i取得 12340*/window.onload = function (){var aLi = document.getElementsByTagName(‘li‘);var arr = [ ‘red‘, ‘yellow‘, ‘blue‘ ];for( var i=0; i<aLi.length; i++ ){aLi[i].index = i;aLi[i].style.background = arr[i%arr.length];aLi[i].onmouseover = function (){this.style.background = ‘gray‘;};aLi[i].onmouseout = function (){//离开之后再返回为原来的颜色。第一种方法利用%来计算this.style.background = arr[this.index%arr.length];// for 里面的函数,不能再直接利用i了。用个索引值利用模%};}};</script><style>li { height:24px; margin-bottom:3px; list-style:none; }</style></head><body><ul id="ul1"><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul></body></html>
var str = ‘‘;aLi[i].onmouseover = function (){str = this.style.background; // 先存颜色this.style.background = ‘gray‘;};aLi[i].onmouseout = function (){this.style.background = str;};
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script> window.onload = function (){ var aInp = document.getElementsByTagName(‘input‘); // aInp[1].checked = false; // aInp[2].checked = true; aInp[0].onclick = function (){ for( var i=1; i<aInp.length; i++ ){ aInp[i].checked = !aInp[i].checked; // !很容易的实习反选 /* if( aInp[i].checked ) { aInp[i].checked = false; } else { aInp[i].checked = true; } */ } }; }; </script> </head> <body> <input type="button" value="反选" /> <ul> <li><input type="checkbox" /></li> <li><input type="checkbox" /></li> <li><input type="checkbox" /></li> <li><input type="checkbox" /></li> <li><input type="checkbox" /></li> </ul> </body> </html>
标签:
原文地址:http://www.cnblogs.com/zhrn/p/4552314.html