简体   繁体   English

Javascrript onclick更改图片标题

[英]Javascrript onclick change image title

I need to display the title of a currently loaded video under the player. 我需要在播放器下显示当前加载的视频的标题。 The user clik on a thumbnail to load the video on the jwplayer. 用户登录缩略图以在jwplayer上加载视频。 I can only load the first title. 我只能加载第一个标题。

 <div>Now playing: <span id='nowPlaying'>My 40th Aniversary</span> </div> <script type="text/javascript"> function changeTitle(){ var video = document.getElementById('vidTitle').getAttribute('title'); document.getElementById('nowPlaying').innerHTML = video; } </script> <!-- body of my pge --> <h2>Trips</h2> <ul> <li> <a href="javascript:loadVideo('rtmp://OnDemand/triptoElgin.mp4');"> <img src="http://www.example.com/images/triptoelgin.jpg" id='vidTitle' onclick='changeTitle()' title="Trip to Elgin "/></a>Trip to Elgin</li> <li> <a href="javascript:loadVideo('rtmp://OnDemand/triptoWaco.mp4');"> <img src="http://www.example.com/images/triptowaco.jpg" id='vidTitle' onclick='changeTitle()' title="Trip to Waco"/></a>Trip to Waco</li> </ul> <!-- begin snippet: js hide: false --> 

How can I add a variable to load the next video title? 如何添加变量以加载下一个视频标题? I tried this, but it gives undefined value. 我试过这个,但它给出了未定义的值。

In HTML, the id attribute must be unique. 在HTML中, id属性必须是唯一的。 Right now, when you're calling document.getElementById('vidTitle') , the browser is picking the first one no matter what. 现在,当你调用document.getElementById('vidTitle')document.getElementById('vidTitle') ,浏览器都会选择第一个。 If you want to identify which of the images you clicked on, I would send this to the changeTitle function like so: 如果你想确定你点击的图像,我会送thischangeTitle像这样的功能:

JavaScript JavaScript的

function changeTitle(element){
  var video = element.getAttribute('title');
  document.getElementById('nowPlaying').innerHTML = video;
}

HTML HTML

<ul>
  <li> <a href="javascript:loadVideo('rtmp://OnDemand/triptoElgin.mp4');"><img src="http://www.example.com/images/triptoelgin.jpg" id='vidTitle1' onclick='changeTitle(this)' title="Trip to Elgin "/></a>Trip to Elgin</li>
  <li> <a href="javascript:loadVideo('rtmp://OnDemand/triptoWaco.mp4');"> <img src="http://www.example.com/images/triptowaco.jpg"  id='vidTitle2' onclick='changeTitle(this)' title="Trip to Waco"/></a>Trip to Waco</li>
</ul>
    function changeTitle(element){
  var video = element.getAttribute('title');
  document.getElementById('nowPlaying').innerHTML = video;
}

 <div>Now playing: <span id='nowPlaying'>My 40th Aniversary</span> </div> <script type="text/javascript"> function changeTitle(video){ var video = video.getAttribute('title'); document.getElementById('nowPlaying').innerHTML = video; } </script> <!-- body of my pge --> <h2>Trips</h2> <ul> <li> <a href="javascript:loadVideo('rtmp://OnDemand/triptoElgin.mp4');"> <img src="http://www.example.com/images/triptoelgin.jpg" onclick='changeTitle(this)' title="Trip to Elgin "/></a>Trip to Elgin</li> <li> <a href="javascript:loadVideo('rtmp://OnDemand/triptoWaco.mp4');"> <img src="http://www.example.com/images/triptowaco.jpg" onclick='changeTitle(this)' title="Trip to Waco"/></a>Trip to Waco</li> </ul> <!-- begin snippet: js hide: false --> 

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

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