简体   繁体   English

文本 - 鼠标悬停在单词上 - 更改颜色,单击单词 - 播放音频,鼠标移出 - 更改文本颜色

[英]Text - on mouse over an word - change the color, on click over the word - play audio, on mouse out - change text color

I found difficult to make it work because im not familiar with js.我发现很难让它工作,因为我不熟悉 js。

I want to write an an line using many words, but every word have own functionality.我想用很多单词写一行,但每个单词都有自己的功能。 Example: On mouse over word-1 change the text color to red, on click play audio-1, on mouse out change the text color to black again.示例:鼠标悬停在 word-1 上时将文本颜色更改为红色,单击播放音频 1 时,鼠标移出时将文本颜色再次更改为黑色。 This way for every word on the line, using different audio files for different words.这样就行的每个单词,对不同的单词使用不同的音频文件。

Right now i used ty this, but i cant manage it to work.现在我用了这个,但我无法让它工作。 Any help?有什么帮助吗?

      <script>
function playAudio(url) {
  new Audio(url).play();
}
    </script>

<br><font onmouseover="this.style.color='red';" onmouseout="this.style.color='black';" onclick="playAudio('audio1.mp3')">Word1</br>
   
<br><font onmouseover="this.style.color='red';" onmouseout="this.style.color='black';" onclick="playAudio('audio2.mp3')">word2</br>
   
<br><font onmouseover="this.style.color='red';" onmouseout="this.style.color='black';" onclick="playAudio('audio3.mp3')">word3<br>

It doesn't work because the tag is outdated.它不起作用,因为标签已过时。 It does not function in HTML5.它在 HTML5 中不起作用。

You can use <p> to make your functions work:您可以使用<p>使您的功能正常工作:

    <script>
  function playAudio(url) {
    new Audio(url).play();
  }
      </script>
  
  <br><p onmouseover="this.style.color='red';" onmouseout="this.style.color='black';" onclick="playAudio('audio1.mp3')">Word1</p</br>
     
  <br><p onmouseover="this.style.color='red';" onmouseout="this.style.color='black';" onclick="playAudio('audio2.mp3')">word2</p></br>
     
  <br><p onmouseover="this.style.color='red';" onmouseout="this.style.color='black';" onclick="playAudio('audio3.mp3')">word3</p><br>

The <p> paragraphs have a 100% width (the whole page) so their with has to be set to something smaller or fit-content . <p>段落具有 100% 的宽度(整个页面),因此它们的 with 必须设置为更小或fit-content Otherwise you will be able to hover or click on the elements at positions where there is no text.否则,您将能够悬停或单击没有文本位置的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM