在另一个函数中使用从模板生成的 id
Using generated id from template in another function?
这里有点麻烦。我需要在单击我拥有的图像时播放音频文件。在我的模板中设置了一堆图像 + 它们的腐蚀性音频文件(有效):
<template>
<div layout horizontal wrap center center-justified class="asdf">
<template repeat="{{s in carddata}}">
<sound-card>
<img src="{{s.imgurl}}" on-click="{{playaudio}}">
<span>{{s.quote}}</span>
<audio id="{{s.soundId}}" src="{{s.soundurl}}" controls preload="auto" autobuffer></audio>
</sound-card>
</template>
</div>
</template>
我将音频元素设置为 display: none
因此当您单击图像时,它只会播放音频而不是播放按钮。到目前为止,一切顺利,但是!我对此模板的所有值都存储在此数组中:
<script>
Polymer('card-container', {
carddata: [],
ready: function() {
this.carddata = [
{imgurl: '../www/img/soundcard-imgs/img1.jpg', quote: 'What?', soundurl: '../www/card-sounds/sound1.m4a', soundId: 'sound1'},
{imgurl: '../www/img/soundcard-imgs/img2.jpg', quote: 'Tickle tickle!', soundurl: '../www/card-sounds/sound2.m4a', soundId: 'sound2'}
];
},
playaudio: function() {
var audioId = //need to get ID here somehow
audioId.play();
}
});
</script>
如您所见,我需要获取该特定<sound-card>
的{{s.soundId}}
并将其传递给playaudio
函数,以便为正确的图像播放正确的声音。我一直在寻找,但找不到获取数组数据绑定生成的 id 的方法。
有什么提示吗?谢谢!
try event.target.templateInstance.model.s.soundId
我不确定您的代码在应用程序的上下文中是什么,所以我做了一些假设(希望是好的假设)。这是一个代码笔,为您提供更通用的解决方案。
一个简单的播客示例
聚合物元素进口(为简单起见合并)<!-- In reference to : http://stackoverflow.com/questions/28978974/using-generated-id-from-template-in-another-function -->
<polymer-element name="sound-card"
attributes="image url quote">
<template>
<style>
:host div {
margin-left: 1em;
}
:host img {
margin-right: 1em;
}
:host h3 {
display: block;
white-space: wrap;
font: bold 1.5em/1 Arial, san-serif;
}
</style>
<div layout horizontal>
<img src="{{image}}"
alt="Audio cover picture"
on-click="{{playAudio}}">
<section>
<h3>{{quote}}</h3>
<audio controls preload="auto" autobuffer>
<source src="{{url}}" type="audio/mpeg">
</audio>
</section>
</div>
</template>
<script>
Polymer('sound-card', {
playAudio: function() {
this.shadowRoot.querySelector('audio').play();
}
});
</script>
</polymer-element>
<polymer-element name="play-list"
attributes="list">
<template>
<style>
:host div {
margin: 2em;
}
:host sound-card {
margin-bottom: 1em;
}
</style>
<div layout horizontal wrap center>
<template repeat="{{sound in sounds}}">
<sound-card image="{{sound.image}}"
quote="{{sound.quote}}"
url="{{sound.url}}">
</sound-card>
</template>
</div>
</template>
<script>
Polymer('play-list', {
listChanged: function(oldVal, newVal){
this.sounds = JSON.parse(newVal);
},
ready: function(){
this.sounds = JSON.parse(this.list);
}
});
</script>
</polymer-element>
主 JS 文件 var data = [
{
"image" : "http://static.libsyn.com/p/assets/7/d/2/e/7d2eff1d932bd82f/episode-34-icon-small.png",
"quote" : "34: Tenon.io & Web Accessibility",
"url" : "http://traffic.libsyn.com/thewebplatform/episode-34_tenon-and-web-accessibility.mp3"
},
{
"image" : "http://static.libsyn.com/p/assets/2/e/0/c/2e0cacdeefe15ec0/episode-32-small.png",
"quote" : "32: Microsoft Spartan & Internet Explorer",
"url" : "http://traffic.libsyn.com/thewebplatform/episode-32_microsoft-spartan-and-internet-explorer.mp3"
}
];
document.querySelector('play-list').setAttribute('list', JSON.stringify(data));
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- Javascript复选框函数:;缺少:在属性id之后"
- javascript点击函数不;不适用于ID和Class
- 将参数中类似形式的ID传递给函数click()
- JavaScript-onClick将按钮ID或单击按钮的文本传递给函数
- 不使用id的javascript中的onfocus函数
- 如何在Jquery函数中为自动完成文本区域指定一个变量作为Id
- 使用jQuery来foreach iframe-src,并通过函数获取ID和操作
- 如何传递元素's ID作为使用getElementById的函数的参数
- 将外部函数返回的id传递给内部函数
- 如何在具有相同 ID 的 Javascript 中按排序函数对关联数组进行排序
- AngularJS-点击对象,访问它's ID&以该ID作为参数激发函数
- 在具有多个值的 PHP 函数中传递 ID
- 如何在 javascript 文件函数中找到 Radnotification id
- 按类划分的元素数组 - 类型错误:$.id 不是一个函数
- HTML span id 作为 JavaScript 函数参数
- 悬停时函数在具有相同 id 的多个元素中不起作用
- 在另一个函数中使用从模板生成的 id
- 如何通过查找函数及其 ID 访问和操作 HTML 表
- id函数不起作用,如果我用户jQuery加载页面