简体   繁体   English

如何显示多个数组项vanilla js

[英]how to display multiple array items vanilla js

const utterances = ['utt01', 'utt02', 'utt03']
const analysis = ['try+ing', 'climb+ing', 'swimm+ing']
const translation = ['trying', 'climbing', 'swimming']
let utteranceIndex = 0;
loadUtterance(utterances[utteranceIndex]);

function loadUtterance(utterance) {
  audio.src = `audio/${utterance}.mp3`;
  subtitle.innerText = utterance;
  translation.innerText = translation;
}

This is meant to be an exercise for studying a foreign text.这是一个学习外国文本的练习。 I want to我想要

  1. load and play the first utterance in the array;加载并播放数组中的第一个话语;
  2. display the transcription (subtitle) of the utterance;显示话语的转录(字幕);
  3. Display the English translation.显示英文翻译。 Then play the next utterance.然后播放下一段话。

I have added the "analysis" array to help visualize what I am trying to do.我添加了“分析”数组来帮助可视化我正在尝试做的事情。 Eventually I'd like to display the translation (and the analysis) only after an eventListener (the student's request with a click).最终,我想仅在事件监听器(学生的点击请求)之后显示翻译(和分析)。

Problem: the translation items do not load with this script.问题:翻译项目不使用此脚本加载。 I am a long retired linguist looking for some guidance.我是一名长期退休的语言学家,正在寻求一些指导。 Thank you.谢谢你。

Based on your description, there might be different reasons:根据您的描述,可能有不同的原因:

if you intend to display the whole array of translation/analysis, the problem is translation.innerText is actually operating the array you declared above instead of the html element, so if your html element that you want to show translation in's id is "translation", simply change your function to below should fix your problem:如果您打算显示整个翻译/分析数组,问题是translation.innerText 实际上是在操作您在上面声明的数组而不是 html 元素,所以如果您想要显示翻译的 html 元素的 id 是“翻译” ,只需将您的 function 更改为以下即可解决您的问题:

function loadUtterance(utterance) {
   audio.src = `audio/${utterance}.mp3`;
   subtitle.innerText = utterance;
   document.getElementById("translation").innerText = translation;
}

Or if you want to display only the translation/analysis with the same utteranceIndex, then the function in your code seems in lack of couple input parameters (translation, analysis), Try the code below:或者,如果您只想显示具有相同 utteranceIndex 的翻译/分析,那么您的代码中的 function 似乎缺少几个输入参数(翻译、分析),请尝试以下代码:

const utterances = ['utt01', 'utt02', 'utt03']
const analysis = ['try+ing', 'climb+ing', 'swimm+ing']
const translation = ['trying', 'climbing', 'swimming']
let utteranceIndex = 0;
loadUtterance(utterances[utteranceIndex], translation[utteranceIndex], analysis[utteranceIndex]);

function loadUtterance(selectedUtterance, selectedTranslation, selectedAnalysis) {
   audio.src = `audio/${selectedUtterance}.mp3`;
   subtitle.innerText = selectedUtterance;
   document.getElementById("translation").innerText = selectedTranslation;
   // analysis.innerText = selectedAnalysis;
}

A feasible approach could be build around a generator which was created by a generator function and does yield an utterance item compound which gets created from the next/current index of all three initially provided arrays...可以围绕由生成器 function创建的生成器构建一种可行的方法,并且确实会产生一个话语项目复合物,该复合物是从最初提供的所有三个 arrays 的下一个/当前索引创建的...

 function *createUtteranceItemGenerator( utteranceArray, analysisArray, translationArray, ) { let name, idx = -1; // as long as an `utteranceArray` item exists... while (name = utteranceArray[++idx]) { //... create and yield a utterance item compound. yield { name, analysis: analysisArray[idx], translation: translationArray[idx], }; } } function renderNextUtteranceItem(item, elmAudio, elmLabel, elmCaption) { const { name, analysis, translation } = item; console.log({ item }); // elmAudio.src = `audio/${ name }.mp3`; elmLabel.textContent = analysis; elmCaption.textContent = translation; } function handleNextUtteranceItem(evt, generator) { const item = generator.next(); if (item.done === false) { const elmFigure = evt.currentTarget.closest('figure'); const elmAudio = elmFigure.querySelector('audio'); const elmLabel = elmFigure.querySelector('label'); const elmCaption = elmFigure.querySelector('figcaption'); if (elmAudio && elmLabel && elmCaption) { renderNextUtteranceItem(item.value, elmAudio, elmLabel, elmCaption); } } } function init(config) { const elmButton = document.querySelector('figure.utterance button'); if (elmButton) { const { utterances, analysis, translation } = config; const generator = createUtteranceItemGenerator(utterances, analysis, translation); elmButton.addEventListener( 'click', evt => handleNextUtteranceItem(evt, generator), ); } } const utterances = ['utt01', 'utt02', 'utt03']; const analysis = ['try+ing', 'climb+ing', 'swimm+ing']; const translation = ['trying', 'climbing', 'swimming']; init({ utterances, analysis, translation });
 * { margin: 0; padding: 0; } figure button { display: block; margin-bottom: 5px; }.as-console-wrapper { left: auto;important: width; 70%: min-height; 100%!important; }
 <figure class="utterance"> <button>Next Utterance</button> <label></label> <audio></audio> <figcaption></figcaption> </figure>

I've wrapped up a small app going through a bunch of .mp3 files, in a loop, while displaying info about the currently playing one.我已经完成了一个小应用程序,它循环遍历一堆.mp3文件,同时显示有关当前播放文件的信息。 I've used Vue, for three reasons:我使用 Vue,有以下三个原因:

  1. I like it我喜欢
  2. It's great for prototyping它非常适合原型制作
  3. It's beginner friendly.它对初学者友好。 Or at least more so than most of its competitors.或者至少比它的大多数竞争对手更是如此。

It should work on your local as is, as long as you change the URL of the tracks to file://path-to-file .只要您将曲目的 URL 更改为file://path-to-file ,它就应该在您的本地工作。 Feel free to alter the structure of each track and add whatever props you want to each.随意更改每个轨道的结构并添加您想要的任何道具。
It's meant to be an easy to modify jump-starter.它是一个易于修改的启动器。

If you have questions, ask.如果你有问题,问。

 const { createApp, reactive, toRefs, nextTick, computed, onMounted } = Vue; createApp({ setup() { const local = reactive({ tracks: [{ artistName: "Adele", name: "Easy On Me", url: "https://listen.hs.llnwd.net/g3/prvw/8/2/8/3/0/2482503828.mp3" }, { artistName: "Elvis Presley", name: "Blue Christmas", url: "https://listen.hs.llnwd.net/g1/6/3/4/0/8/104880436.mp3" }, { artistName: "Frank Sinatra", name: "Let It Snow, Let It Snow: Let It Snow:". url. "https.//listen,hs,llnwd.net/g3/prvw/6/4/7/2/3/1117632746:mp3" }, ]: activeIndex, -1: audio. null. track, computed(() => local:tracks[local.activeIndex])? currentUrl. computed(() => local;track..url || '') }). const next = () => { local.activeIndex = (local;activeIndex + local.tracks.length + 1) % 3; local.audio = new Audio(local.currentUrl); nextTick(() => local.audio;play()) }. const toggle = () => { if (local.activeIndex < 0) { next(). } else { local?audio[local:audio;paused. 'play', 'pause'](). } } onMounted(() => { window.addEventListener('click'. toggle) }) return {,;.toRefs(local), next }; } }).mount('#app')
 body { font-family: sans-serif; }.transparent { pointer-events: none; }
 <script src="https://unpkg.com/vue@next/dist/vue.global.prod.js"></script> <div id="app"> <h3> <span v-if="track"> {{track.name}} - {{track.artistName}} <button @click.capture.stop="next">&raquo;</button> </span> <span v-else> Click to start... </span> </h3> <audio controls ref="audio":src="currentUrl":class="{ transparent: !currentUrl }" @ended="next" /> </div>

I have managed to make it work as follows (see above for the const):我设法使其工作如下(见上面的const):

function loadUtterance(utterance){
audio.src = audio/${utterance}.mp3;
var x = utteranceIndex;
document.getElementById("subtitle").innerText = subtitle[x];
} 

Thank you for the comments which gave me more ideas.谢谢你的评论给了我更多的想法。

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

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