<input type="button" value="开灯">
<img id="myImage" src="../img/off.gif">
<input type="button" value="关灯">
<script>
var img = document.getElementById('myImage');
var input = document.getElementsByTagName('input');
input[0].onclick = on;
function on() {
img.src = '../img/on.gif';
}
input[1].onclick = off;
function off() {
img.src = '../img/off.gif';
}
var x = 1;
function show() {
if (x % 2 === 0) {
off();
} else if (x % 2 === 1) {
on();
}
x++;
}
setInterval(show, 1000);
</script>
==========================================
<img src="../img/banner_1.jpg" id="light" alt="ti"/>
<script>
var x = 1;
var img = document.getElementById("light");
setInterval(function () {
if (x % 3 === 1) {
img.src="../img/banner_2.jpg";
}else if(x%3===2){
img.src="../img/banner_3.jpg";
}else if(x%3===0){
img.src="../img/banner_1.jpg";
}
x++;
}, 1000);
</script>
======================================
<style>
span{
color: red;
}
</style>
</head>
<body>
<div style="text-align: center">
<span id="time">5</span>秒之后,跳转到首页。
</div>
<script>
var span = document.getElementById('time');
var x = 5;
setInterval(function () {
x--;
if (x === 0) {
location.href = "https://www.baidu.com";
} else {
span.innerHTML = x;
}
}, 1000);
</script>
=======================================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript演示</title>
</head>
<body>
<input type="button" onclick="on()" value="开灯">
<img id="myImage" border="0" src="../imgs/off.gif" style="text-align:center;">
<input type="button" onclick="off()" value="关灯">
<script>
function on(){
document.getElementById('myImage').src='../imgs/on.gif';
}
function off(){
document.getElementById('myImage').src='../imgs/off.gif'
}
var x = 0;
setInterval(function (){
if(x % 2 == 0){
on();
}else {
off();
}
x ++;
},1000);
</script>
</body>
</html>
|