4 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以添加thisinto onmouseover="playSound(this)",這將傳遞一個(gè)懸停元素。
然后你在函數(shù)中捕獲它:
function playSound(e) {
并用于保存 id,然后只做 if 語句。
let id = e.id;
var sound1 = document.getElementById("audio1");
var sound2 = document.getElementById("audio2");
function playSound(e) {
console.clear();
console.log(e.id);
let id = e.id;
if (id === "x") {
//if hovered image has id="x", do this
sound1.play();
console.log("Playing sound x");
} else if (id === "y") {
//if hovered image has id="y", do this
sound2.play();
console.log("Playing sound y");
}
}
<a href="Okokoska.html"><img src="Pkokoska.jpg" id="x" onmouseover="playSound(this)" onmouseout="stop()"></a>
<a href="Okonj.html"><img src="Pkonj.jpg" id="y" onmouseover="playSound(this)" onmouseout="stop()"></a>
<audio id="audio1"><source src="Zkokoska.wav"></audio>
<audio id="audio2"><source src="Zkrava.wav"></audio>

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
盡量不要使用內(nèi)聯(lián) JavaScript 來了解更多不應(yīng)該使用它只是解決這個(gè)問題所以由于以上原因只需使用一個(gè)addEventListener
或使用偵聽器賦值就像id.onclick = function(){}...
這里是一個(gè)
例子
var sound1 = document.getElementById("audio1");
var sound2 = document.getElementById("audio2");
var x = document.getElementById("x");
var y = document.getElementById("y");
x.onmouseover = () => {
sound1.play();
}
x.onmouseout = () => {
sound1.pause();
}
y.onmouseover = () => {
sound2.play();
}
y.onmouseout = () => {
sound2.pause();
}
<a href="Okokoska.html"><img src="Pkokoska.jpg"></a>
<a href="Okonj.html"><img src="Pkonj.jpg" id="y"></a>
<audio id="audio1"><source src="Zkokoska.wav"></audio>
<audio id="audio2"><source src="Zkrava.wav"></audio>
或者你也可以看到下面的例子希望你會(huì)發(fā)現(xiàn)它有幫助
var button = document.getElementById("button");
var audio = document.getElementById("player");
button.addEventListener("mouseover", function() {
audio.play();
button.innerHTML = "Play";
});
button.addEventListener("mouseout", function() {
audio.pause();
button.innerHTML = "Pause";
});
div {
display: block;
cursor: pointer;
width: 50px;
height: 50px;
background-color: #000000;
}
button {
cursor: pointer;
font-family: Tahoma;
font-weight: bold;
font-size: 14px;
background-color: #00ffff;
color: blue;
padding: 13px 6px;
}
<div>
<button id="button">Play</button>
</div>
<audio id="player">
<source src='http://hi5.1980s.fm/;' type='audio/mpeg'/>
</audio>

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
通過onclick將事件作為參數(shù)傳遞到綁定到img標(biāo)記的playSound函數(shù)中,并在您的邏輯中訪問它以驗(yàn)證 Id。
HTML
<a href="Okokoska.html"><img src="Pkokoska.jpg" id="x" onmouseover="playSound(event)" onmouseout="stop()"></a>
<a href="Okonj.html"><img src="Pkonj.jpg" id="y" onmouseover="playSound(event)" onmouseout="stop()"></a>
<audio id="audio1"><source src="Zkokoska.wav"></audio>
<audio id="audio2"><source src="Zkrava.wav"></audio>
JavaScript
var sound1 = document.getElementById("audio1");
var sound2 = document.getElementById("audio2");
function playSound(event) {
if(event.target.id == "x"){
sound1.play();
}else if(event.target.id == "y"){
sound2.play();
}
}

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
我很早就不喜歡劇本了,但你試過這個(gè)嗎(我沒試過)?
function playSound(){
document.getElementById("x").addEventListener("click", function(){ document.getElementById("audio1").play()[0]; });
document.getElementById("y").addEventListener("click", function(){ document.getElementById("audio2").play()[0]; });
}
添加回答
舉報(bào)