<div class="panel" id="home">
<video id='video1' width='630' autoplay controls>
<source id="source1" src="1_uno.mp4" type="video/mp4">
</video>
</div>
我有这个js函数,在'video1'的和处使div home的刷新放置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 事件我执行刷新div功能:
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>
最佳答案
您应该更改onclick="refreshdiv("a")"
至onclick="refreshdiv(\'a\')"
在你的<button>
标签。 “内部”双引号实际上结束了“外部”,另一种方法是转义内部引号,如下所示: \"
.
关于javascript - 在另一个函数之后使用一个 js 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24457630/