1代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>定时器</title>
</head>
<body>
<input type="text" id="timer">
<button id="start">开始</button>
<button id="end">结束</button>
<script>
var timetag;
// 1 获取当前时间
function f1() {
var showTime = new Date();
var showLocaleTime = showTime.toLocaleString();
var inpEle = document.getElementById('timer');
inpEle.value = showLocaleTime;
}
// 2 把时间放进去
// 2.1 找到strat开始按钮,绑定点击事件
var startBtn = document.getElementById('start');
startBtn.onclick = function () {
//2.2 找到input标签,并将值放到input标签里面
f1();
if (timetag === undefined){
timetag = setInterval(f1,1000);
}
};
// 3 停止时间
var endBtn = document.getElementById('end');
endBtn.onclick = function () {
clearInterval(timetag);
timetag = undefined;
}
</script>
</body>
</html>
2代码:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>select联动</title>
</head>
<body>
<select id="province">
<option>请选择省:</option>
</select>
<select id="city">
<option>请选择市:</option>
</select>
<script>
var data = {"河北省": ["廊坊", "邯郸"], "北京": ["朝阳区", "海淀区"], "山东": ["威海市", "烟台市"]};
// 1 将省份的数据放到省份的下拉框里面
//1.1 找到省份下拉框
var proSelect = document.getElementById('province');
// 1.2 创建option标签
//1.3 将数据放到option标签中,并将option标签放到省份下拉框里面
for (var province in data){
var proOption = document.createElement('option');
proOption.innerText = province;
proSelect.appendChild(proOption);
}
//2 选择省份,将被选择的省份的市都放到市的那个下拉框里面
var citySelect = document.getElementById('city');
proSelect.onchange = function () {
citySelect.innerText = '';
var proText = this.options[this.selectedIndex].innerText;
var cityData = data[proText];
for (var cityindex in cityData){
var cityOption = document.createElement('option');
cityOption.innerText = cityData[cityindex];
citySelect.appendChild(cityOption);
}
}
</script>
</body>
</html>