将动态div内容转换为链接

时间:2011-11-19 12:20:10

标签: javascript css html

我正在使用Longtail的JW Player,我遇到了一些基本的问题。我不知道在编程语言中它叫什么,这就是为什么我会一步一步地写它:

有一个javascript代码可以显示播放视频的标题和说明,如下所示

<script type="text/javascript">
  var player    =  null;
  var playlist  =  null;

  function playerReady(obj)
  {
    player = gid(obj.id);
    displayFirstItem();
  };

  function displayFirstItem()
  {
    try
    {
      playlist = player.getPlaylist();
    }
    catch(e)
    { 
      setTimeout("displayFirstItem()", 100);
    }

      player.addControllerListener('ITEM', 'itemMonitor');
      itemMonitor({index:player.getConfig()['item']});
  };
  function itemMonitor(obj)
  {
    gid('nowplaying').innerHTML = 'Playing: ' + playlist[obj.index]['title'] + ''; 
    gid('author').innerHTML = '<p>Author: ' + playlist[obj.index]['author'] + '</p>'; 
    gid('description').innerHTML = '<p>Description: ' + playlist[obj.index]['description'] + '</p>'; 
  };
  function gid(name)
  {
    return document.getElementById(name);
  };
</script>

代码将视频标题返回到div:

<div id="nowplaying"></div>

我想要的是在推文中显示视频标题:

href="http://twitter.com/home?status=SONG TITLE"

我该怎么做?最好的问候

1 个答案:

答案 0 :(得分:2)

编辑itemMonitor()功能:

  function itemMonitor(obj)
  {
    gid('nowplaying').innerHTML = 'Playing: ' + playlist[obj.index]['title'] + ''; 
    gid('author').innerHTML = '<p>Author: ' + playlist[obj.index]['author'] + '</p>'; 
    gid('description').innerHTML = '<p>Description: ' + playlist[obj.index]['description'] + '</p>';
    gid('tweetLink').href = 'http://twitter.com/home?status=' + encodeURIComponent(playlist[obj.index]['title']);
  };

这要求文档中包含id="tweetLink"的链接,这不会改变链接的文本,但是,如果要更新链接的文本:

  function itemMonitor(obj)
  {
    gid('nowplaying').innerHTML = 'Playing: ' + playlist[obj.index]['title'] + ''; 
    gid('author').innerHTML = '<p>Author: ' + playlist[obj.index]['author'] + '</p>'; 
    gid('description').innerHTML = '<p>Description: ' + playlist[obj.index]['description'] + '</p>';
    gid('tweetLink').href = 'http://twitter.com/home?status=' + encodeURIComponent(playlist[obj.index]['title']);
    gid('tweetLink').innerHTML = 'Tweet this song: ' + playlist[obj.index]['title'] + '.';
  };