问题描述
我从网站上的音频开始;我以前在我的其中一种视图中实现了带有字形按钮的audio_tag.用另一种观点,我试图列出我所有的单词对象,并且我想有一个按钮来播放与它们相关的每个音频.到目前为止,此功能仅适用于此视图中列出的第一个单词对象.点击视图中的所有对象后,如何使按钮播放声音?
I'm starting out with audio on my site; I've previously implemented an audio_tag with a glyph button in one of my views. In a different view, I'm trying to list all of my word objects, and I would like to have a button to play each of their associated audio. As of now, this functionality only works for the very first word object listed in this view. How can I make the buttons play the sound upon click for all of the objects in the view?
课程展示视图:
<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:
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();
});
});
推荐答案
为每一个赋予自己唯一的ID.
Give each one it's own unique id.
在html中,id必须是唯一的.例如#audioButton1"#audioButton2"等...或者,不要使用id,使用类名,例如 class ="btn btn-btn-default audio-button"
然后是 $(".audio-button).on(" click,function()
In html ids must be unique. eg "#audioButton1" "#audioButton2" etc... alternatively, don't use ids, use class names eg class="btn btn-default audio-button"
then $(".audio-button").on("click", function()
这篇关于仅顶部的audio_tag按钮播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!