仅播放top audio_tag按钮

我开始在我的网站上播放音频; 我以前在我的一个视图中实现了一个带有字形按钮的audio_tag。 在另一个视图中,我正在尝试列出我的所有单词对象,并且我希望有一个按钮来播放每个相关的音频。 截至目前,此function仅适用于此视图中列出的第一个单词对象。 如何在按钮中为视图中的所有对象播放声音?

课程显示视图:

Vocabulary


  • ()

application.js中:

 // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require bootstrap-sprockets //= require jquery_ujs //= require_tree . jQuery(document).ready(function() { $("#audioButton").on("click", function() { $(".audio-play")[0].currentTime = 0; return $(".audio-play")[0].play(); }); }); 

给每个人一个自己独特的身份。

在HTML中,ID必须是唯一的。 例如“#audioButton1”“#audioButton2”等…或者,不要使用id,使用类名,例如class="btn btn-default audio-button"然后$(".audio-button").on("click", function()