JS基础特效 (一)
一、动态获得时间
具体思路就是,先用JS获得当前时间的时间戳,再用将时间戳转换成当前的时间,将时间添加到HTML的标签内部去就行;还有,你可以直接调用函数来获取当前的年月日就行。
<div id="timeDiv"></div>
<button onclick=”getTime()">显示时间</button>
<script>
获取当前时间
function getTime(){
//获得当前时间对象
Let date = newDate();//跟Java中创建对象类似
//获得年
Let year = date.getFullYear();
//获得月(从0开始,1月获得的值是0)
Let month = formatNum(date.getMonth()+1);
//获得日
let day = formatNum(date.getDate());
//获得时
Let hour = formatNum(date.getHours());
//获得分
let min = formatNum(date.getMinutes());
//获得秒
Let seconds = formatNum(date.getSeconds());
Let nowTime=year+"年"+month+"月"+day+"日”+hour+":"+min+":"+seconds;
//获得DIV对象,根据id获得一个div对象
var div0bj = document.getElementById("timeDiv" );
div0bj.innerHTML = nowTime;
//使用定时器(延时器)自动调用getTime函数
setTimeout(getTime,1000);
</script>
二、全选和全不选
对于按钮而言,所需功能就是点击了之后就去判断,你点的是哪个按钮,然后通过一个循环将所有的单选框给选中或取消选中。
<div>
<input type="checkbox" name="hobbys" value="吃饭”/>吃饭
<input type="checkbox" name="hobbys" value="购物"/>购物
<input type="checkbox" name="hobbys" value="听音乐"/>听音乐
<input type="checkbox" name="hobbys" value="网游"/>网游
<input type="checkbox" name="hobbys" value="打球"/>打球
<br/>
<button onclick="selector(true)">全选</button>
//οnclick="selector(true)"很重要
<button onclick="selector(false)">全不选</button>
</div>
<script>
function selector(boo){
//通过name属性获得一组对象
var hobbyArr = document.getElementsByName("hobbys");
循环设置checkbox的checked属性为true
for(var hobby of hobbyArr){
//检测元素兑现过的类型是否为checkbox类型
if (hobby.type =="checkbox" ){
hobby.checked=boo ;
}
}
</script>