script追加新元素
window.onload = function(){ //init()
document.getElementById("addCitySender").onclick = function(){ //button的onclick匿名函数
// alert("123");
var newCityWord = document.createTextNode("深圳"); //创建一个只有文字的节点
var newEle = document.createElement("li"); //创建一个元素 <li></li>
newEle.appendChild(newCityWord); //元素中加入节点<li>深圳</li>
var list = document.getElementById("ul_list"); //获取list
list.appendChild(newEle); //追加到现有的list中
}
}
script全选全不选
function selectOrUnselectAllAction(){
var isChcked = document.getElementById("selectOrUnselectAll").checked;
var eles = document.getElementsByName("checkOnebox");
for (var i = 0; i < eles.length; i++) {
if(eles[i].checked != isChcked){
eles[i].checked = isChcked;
}
}
}
script获取table中的row元素 并设置style
window.onload = function () {
document.getElementById("table").tHead.style.backgroundColor = "gray";
var tableRows = document.getElementById("table").tBodies[0].rows;
for (var i = 0; i < tableRows.length; i++) {
if(i % 2 == 0){
tableRows[i].style.backgroundColor = "pink";
}else{
tableRows[i].style.backgroundColor = "yellow";
}
}
}
省市选择 onchange / object.options.length = 0;
//html
<td> //select标签值改变的事件 onchange 传入当前选择的值 -- this.value
<select id="province" onchange="changeprovinceAction(this.value)">
<option>---请选择----</option>
<option value="0">安徽</option>
<option value="1">江苏</option>
<option value="2">浙江</option>
</select>
<select id="city">
</select>
//script
var citys = new Array();
citys[0] = ["亳州","合肥","芜湖","安庆"];
citys[1] = ["南京","苏州","无锡","常州"];
citys[2] = ["杭州","慈溪","金华","义务"];
function changeprovinceAction(prove){
var cityEle = document.getElementById("city");
cityEle.options.length = 0; //将上一个省份的城市置空
for (var i=0;i<citys.length;i++) {
if(i==prove){
for (var j = 0; j<citys[i].length;j++) {
var city = document.createTextNode(citys[i][j]);
var ele = document.createElement("option");
ele.appendChild(city);
cityEle.appendChild(ele);
}
break;
}
}
}
开始编辑 结束编辑 onfocus / onblur / innerHTML
function onfocusAction(eleId,specificId){
var spanTips = document.getElementById(eleId + "Tips");
var tipsStr;
switch(specificId){
case 0: {
tipsStr = '用户名必填!'; break;
}
case 1: {
tipsStr = '密码必填!';break;
}
}
spanTips.innerHTML = ('<font color="gray">'+tipsStr+'</font>');
}
function onblurAction(eleId,specificId){
var targetEle = document.getElementById(eleId);
var spanTips = document.getElementById(eleId + "Tips");
var tipsStr;
switch(specificId){
case 0: {
if (targetEle.value ==""){
tipsStr = '用户名不能为空!';
}else if(targetEle.value.length > 10){
tipsStr = '用户名不能超过10个字!';
}else{
tipsStr = '';
}
break;
}
case 1: {
if (targetEle.value ==""){
tipsStr = '密码必填!';
}else{
tipsStr = '';
}
break;
}
}
spanTips.innerHTML = ('<font color="red">'+tipsStr+'</font>');
}