|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<script>
var arr=[-2,-1,0,1,2,3];
// var n=parseInt(prompt());
// for(var i=0;i<n;i++) arr[i]=parseInt(prompt());//自定义输入
function fil(arr,fn){
var newarr=[];
for(var i=0;i<arr.length;i++)
{
if(fn(arr[i])) newarr.push(arr[i]);
}
return newarr;
}
function fn(a)
{
if(a>0) return true;
else return false;
}
document.write(fil(arr,fn));
</script>
</body>
</html>
-------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编程题答案1</title>
</head>
<body>
<style>
span {
display: inline-block;
width: 80px;
height: 80px;
border-radius: 50%;
border: 1px solid white;
opacity: 0.1;
}
.light {
opacity: 1;
}
#red {
background-color: red;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#time {
margin-left: 30px;
font-size: 70px;
}
</style>
编程题答案1
<p>
<span id="red" class="light"></span>
<span id="yellow"></span>
<span id="green"></span>
<b id="time">10</b>
</p>
<script>
var current = 'red';
var time = 10;
function changeLight(from, to, timeout) {
if (time > 0) {
return;
}
current = to;
time = timeout;
document.getElementById(from).removeAttribute('class', 'light');
document.getElementById(to).setAttribute('class', 'light');
}
setInterval(function() {
time--;
if (current === 'red') {
changeLight('red', 'yellow', 3);
} else if (current === 'yellow') {
changeLight('yellow', 'green', 8);
} else if (current === 'green') {
changeLight('green', 'red', 10);
}
document.getElementById('time').innerText = time;
}, 1000);
</script>
</body>
</html>
-----------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>实现计时器</h1>
<p>
<span id = "hour">00</span>
<span>:</span>
<span id = "minute">00</span>
<span >:</span>
<span id = "second">00</span>
</p>
<button id = "start"> 开始</button>
<button id = "pause"> 暂停</button>
<button id = "stop"> 停止</button>
<script>
var hour = 0 ;
var minute = 0 ;
var second = 0 ;
var intervalId;
function getEleById(id){
return document.getElementById(id) ;
}
function addZero(num){
return num >= 10 ? num : '0' + num ;
}
function setTimeOnDOM() {
getEleById('hour').innerText = addZero(hour) ;
getEleById('minute').innerText = addZero(minute) ;
getEleById('second').innerText = addZero(second) ;
}
getEleById('start').onclick = function() {
if(intervalId){
return;
}
intervalId = setInterval(function() {
second++ ;
if(second >= 60){
second = 0 ;
minute++ ;
}
if(minute >= 60 ){
minute = 0 ;
hour++ ;
}
if(hour >= 24){
clearInterval(intervalId)
}
setTimeOnDOM();
} , 1000)
}
getEleById('pause').onclick = function(){
clearInterval(intervalId);
intervalId = null ;
}
getEleById('stop').onclick = function(){
clearInterval(intervalId) ;
intervalId = null ;
hour = minute = second = 0 ;
setTimeOnDOM() ;
}
</script>
</body>
</html> |
|