Javascript按钮切换不适用于第一次单击,仅第二次或更多次

时间:2020-06-27 22:06:17

标签: javascript button toggle

我不确定这是怎么回事,但是我在视频的左上方有一个javascript切换按钮,用于交换视频嵌入源并将切换的隐藏式字幕文本从“ cc on”到“ cc off”并返回...,但只有在初次点击后才能使用。我在这里想念什么?

控制台在页面加载时显示“未定义的按钮”,但是我不确定哪个不正确。

HTML:

<div class="navbar">
  <button id="togglecc" value="OFF" onclick="toggle(this);">cc on</button>
</div>
<div id="player">
  <div id="la1-video-player" data-embed-id="87e859b2-5db8-4ad8-bac7-575c22288930"></div>
  <script type="application/javascript" src="https://control.livingasone.com/webplayer/loader.min.js"></script>
</div>

脚本:

function toggle(button) {
  v = document.getElementById("togglecc");
  vp = document.getElementById("player");
  if (v.value == "ON") {
    v.value = "OFF";
    v.innerHTML = 'cc off';
    vp.innerHTML = '<div id="la1-video-player" data-embed-id="9257a531-25ce-48bd-821b-9335681c45d8"></'+'div>';var a=document.createElement("script");a.setAttribute("src","//control.livingasone.com/webplayer/loader.min.js");document.getElementById('player').appendChild(a);
  } else {
    v.value = "ON";
    v.innerHTML = 'cc on';
    vp.innerHTML = '<div id="la1-video-player" data-embed-id="87e859b2-5db8-4ad8-bac7-575c22288930"></'+'div>';var a=document.createElement("script");a.setAttribute("src","//control.livingasone.com/webplayer/loader.min.js");document.getElementById('player').appendChild(a);
  }
}

https://jsfiddle.net/milkboy31/tg02Lv4c/

1 个答案:

答案 0 :(得分:0)

这是您更新的小提琴

toggle button

同样,您应该避免在按钮元素上调用函数

%%timeit
s = df['Data'].isnull()
df['sum'] = df.groupby(s.cumsum())['Data'].cumsum().where((~s) & (s.shift(-1)))
4.52 ms ± 901 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
g = df.reset_index().groupby(df['Data'].isnull().cumsum())
df['sum'] = g['Data'].cumsum().where(lambda x: x.index == g['index'].transform('idxmax'))
8.52 ms ± 1.45 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
blocks = df['Data'].isnull().cumsum()
m = blocks.duplicated(keep='last')
df['sum'] = df.groupby(blocks)['Data'].cumsum().mask(m)
3.02 ms ± 172 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)