[英]How to make an onclick in a img html5 tag call a function with a parameter?
I want my img link to call a javascript function that will send a name which will be a video.mp4 name to the function sendName. 我希望我的img链接调用一个JavaScript函数,该函数将向该函数sendName发送一个名为video.mp4的名称。 At this point sendName should just display an alert.
此时,sendName应该只显示一个警报。
In the end I want to call the jwplayer inside of a function that I can send in a parameter with a movie name so that it will play the particular movie. 最后,我想在函数中调用jwplayer,该函数可以发送带有电影名称的参数,以便它将播放特定的电影。 The name valley.mp4 is were the variable will be placed.
名称Valley.mp4是放置变量的位置。 If I put the jwplayer inside of a function then the player will not load on the screen.
如果将jwplayer放在函数中,则播放器将不会加载到屏幕上。
so when I click on a movie name then it will play that movie. 因此,当我单击电影名称时,它将播放该电影。 This is for my own use at home.
这是我自己在家里使用的。
<li class="topfirst"><a href="#" style="width:574px;">
<img src="s/256base-open-over.png" onClick="sendName('a.mp4');">Dr. No. (1962)</a></li>
Other code Here: 其他代码在这里:
jwplayer("test").setup({
playlist: [{
image: "0001.jpg",
sources: [
{file: "valley.mp4",label: "144p"}
]
}]
});
Test function: 测试功能:
function sendName(name) {
alert(name);
}
You could use data attributes to store params. 您可以使用数据属性存储参数。 When the event is fired you the target from event.target and then you could use its data attributes.
触发事件后,您可以使用event.target中的目标,然后可以使用其数据属性。
// based on jQuery
$('img').on('click', function (event) {
var btn = $(event.target);
sendName(btn.data('video-name'));
});
You should follow similar pattern. 您应该遵循类似的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.