在另一个函数之后使用js函数

时间:2014-06-27 17:28:29

标签: javascript jquery html

<div class="panel" id="home">
  <video id='video1' width='630' autoplay controls>
    <source id="source1" src="1_uno.mp4" type="video/mp4">
  </video>
</div> 

我&#39;这个js的功能是在&#39; video1&#39;使div刷新为3个按钮

var myVideo = document.getElementById("video1");
myVideo.addEventListener('ended',myHandler,false);
function myHandler(e) {
  document.getElementById('home').innerHTML = '<button type="button" style="height: 180px; width: 200px"  onclick="refreshdiv("a")"> a </button><button type="button" style="height: 180px; width: 200px" onclick="refreshdiv("b")"> b </button><button type="button" style="height: 170px; width: 200px" onclick="refreshdiv("c")"> c </button>';
}

直到这一部分一切正常,现在你可以看到onclick事件我做了refreshdiv函数:

function refreshdiv(str) {
  if(str=='a'){
    src='1_uno.mp4';
  }
  if(str=='b'){
    src='2_due.mp4';
  }
  if(str=='c'){
    src='3_tre.mp4';
  }
  document.getElementById('home').innerHTML = '<video id="video1" width="630" autoplay controls><source id="source1" src="'+ src +'" type="video/mp4"></video>';
}

问题是,当我点击按钮发生任何事情时,在Chrome中我收到此错误:Unexpected token }

所有功能都在里面:<script></script>

2 个答案:

答案 0 :(得分:2)

您应该在onclick="refreshdiv("a")"代码中将onclick="refreshdiv(\'a\')"更改为<button>。 &#34;内部&#34;双引号实际上是结束&#34;外部&#34;,另一种选择是逃避内部引号,如下所示:\"

答案 1 :(得分:1)

你应该改变 onclick="refreshdiv("a")">onclick="refreshdiv(\'a\')">等。

'<button type="button" style="height: 180px; width: 200px"  onclick="refreshdiv(\'a\')"> a </button><button type="button" style="height: 180px; width: 200px" onclick="refreshdiv(\'b\')"> b </button><button type="button" style="height: 170px; width: 200px" onclick="refreshdiv(\'c\')"> c </button>';

这是一个令人讨厌的例子 http://jsfiddle.net/8RjW5/1/

相关问题