87,996
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Color and Music</title>
<link rel="stylesheet" href="colorAndMusic.css">
</head>
<body>
<table id="colorTable">
<tr>
<th class="_ffffcc" id="a0"> </th>
<th class="_ffcccc" id="a1"> </th>
<th class="_8696a7" id="a2"> </th>
<th class="_fffaf4" id="a3"> </th>
<th class="_ff9999" id="a4"> </th>
<th class="_cc8899" id="a5"> </th>
<th class="_ff9999" id="a6"> </th>
<th class="_cc8899" id="a7"> </th>
</tr>
</table>
<audio src="" id="music"></audio>
<script src="colorAndMusic.js"></script>
</body>
</html>
//鼠标事件:1.播放音乐 2.更改背景色
function mouseEvent_changeColor() {
var tabs = document.getElementsByTagName("th");
var tabsLen = tabs.length;
for(var i=0;i<tabsLen;++i) {
var curColor;
tabs[i].onmouseover = function() {
//更改背景色
curColor = this.style.backgroundColor;
this.style.backgroundColor = "#fff";
//播放音乐
playMusic(this.id);
}
tabs[i].onmouseout = function() {
//改回原本的背景色
this.style.backgroundColor = curColor;
}
}
}
function playMusic(thisId){
var audio = document.getElementById("music");
audio.src = "songs" + "/" + thisId + ".ogg";
console.log(thisId + " " + audio.src);
if(!audio.load()) console.log("不能加载");
audio.load();
if(!audio.play()) console.log("不能播放");
audio.play();
}

function playMusic(thisId){
var audio = document.getElementById("music");
audio.src = "songs" + "/" + thisId + ".ogg";
console.log(thisId + " " + audio.src);
audio.play();
}