HTML代码(我还没有包含图片,文字,音频的代码,因为我知道他们使用onclick功能时很好:
...
<table>
<tbody>
<tr>
<td><img onclick="sleepFunction(this)" src="01.jpg" height="180" width="120"></td>
<td><img id = "02" src="02.jpg" height="180" width="120"/></td>
<td><img id = "03" src="03.jpg" height="180" width="290"/></td>
</tr>
<tr>
<td><img id = "04" src="04.jpg" height="180" width="120"/></td>
<td><img id = "05" src="05.jpg" height="180" width="120"/></td>
<td><img id = "06" src="06.jpg" height="180" width="290"/></td>
</tr>
<tr>
<td><img id = "07" src="07.jpg" height="180" width="120"/></td>
<td><img id = "08" src="08.jpg" height="180" width="120"/></td>
<td><img id = "09" src="09.jpg" height="180" width="290"/></td>
</tr>
</tbody>
</table>
...
这是JavaScript:
function slideShow (text, image, song) {
this.text = text;
this.image = image;
this.song = song;
this.xbutton = xbutton;
this.moveLeft = function(){
this.text.style.left = "357px";
this.image.style.left = "851px";
this.song.style.left = "450px";
this.song.play();
this.xbutton.style.left = "357px";
}
}
var sleep = new slideShow(document.getElementById("sleepText"),document.getElementById("sleepImage"), document.getElementById("sleepSong"), getElementById("xbutton"));
var sleepLeft = sleep.moveLeft();
document.getElementById("02").addEventListener("click", moveLeft);
答案 0 :(得分:1)
看起来你很亲密。 moveLeft
函数位于SlideShow
实例中(您应该将该首字母大写),因此您需要将您的监听器更改为:
document.getElementById("02").addEventListener("click", sleep.moveLeft);
使用大写对象:
function SlideShow (text, image, song) {
this.text = text;
this.image = image;
this.song = song;
this.xbutton = xbutton;
this.moveLeft = function(){
this.text.style.left = "357px";
this.image.style.left = "851px";
this.song.style.left = "450px";
this.song.play();
this.xbutton.style.left = "357px";
}
}
var sleep = new SlideShow(document.getElementById("sleepText"),document.getElementById("sleepImage"), document.getElementById("sleepSong"), getElementById("xbutton"));
var sleepLeft = sleep.moveLeft();
document.getElementById("02").addEventListener("click", sleep.moveLeft);