仅播放top audio_tag按钮

时间:2016-06-14 01:14:38

标签: javascript ruby-on-rails audio

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

课程展示视图:

<div class ="col-xs-10 col-xs-offset-1">
  <h3>Vocabulary</h3>
  <br>
  <ul>
    <% @lesson.words.each do |word| %>
      <li> 
        <%= image_tag word.image_url(:thumb) if word.image? %> 

        <b><%= word.term %></b> 

        <%= audio_tag word.sound, class: "audio-play" %>
        <button type="button" class="btn btn-default", id="audioButton">
          <span class="glyphicon glyphicon-play" aria-hidden="true"></span>
        </button>

        <i>(<%= word.reference %>)</i>

      </li>
      <br>
    <% end %>
  </ul>
</div>

的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();
      });
});

1 个答案:

答案 0 :(得分:1)

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

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