基本上我有这样的代码,它是非常丑陋的,我是一个初学者在HTML,CSS和JS所以裸露与我,
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E2.mp4';getElementById('s2').innerHTML='Season 2 Episode 2'">
Episode 2
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E3.mp4';getElementById('s2').innerHTML='Season 2 Episode 3'">
Episode 3
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E4.mp4';getElementById('s2').innerHTML='Season 2 Episode 4'">
Episode 4
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E5.mp4';getElementById('s2').innerHTML='Season 2 Episode 5'">
Episode 5
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E6.mp4';getElementById('s2').innerHTML='Season 2 Episode 6'">
Episode 6
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E7.mp4';getElementById('s2').innerHTML='Season 2 Episode 7'">
Episode 7
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E8.mp4';getElementById('s2').innerHTML='Season 2 Episode 8'">
Episode 8
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E9.mp4';getElementById('s2').innerHTML='Season 2 Episode 9'">
Episode 9
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E10.mp4';getElementById('s2').innerHTML='Season 2 Episode 10'">
Episode 10
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E11.mp4';getElementById('s2').innerHTML='Season 2 Episode 11'">
Episode 11
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E12.mp4';getElementById('s2').innerHTML='Season 2 Episode 12'">
Episode 12
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E13.mp4';getElementById('s2').innerHTML='Season 2 Episode 13'">
Episode 13
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E14.mp4';getElementById('s2').innerHTML='Season 2 Episode 14'">
Episode 14
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E15.mp4';getElementById('s2').innerHTML='Season 2 Episode 15'">
Episode 15
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E16.mp4';getElementById('s2').innerHTML='Season 2 Episode 16'">
Episode 16
</button>
<button type="button" class="buttons" onclick="document.getElementById('my-video2_html5_api').src = 'S2E17.mp4';getElementById('s2').innerHTML='Season 2 Episode 17'">
Episode 17
</button>
它看起来是如此的拥挤,从搜索中我找不到一种方法来简化JS脚本的代码?
我可以设置一个变量,让x = document等等,但这仍然会把所有东西都聚集起来
5条答案
按热度按时间ipakzgxi1#
将HTML更改为如下所示:
然后,您可以使用JavaScript循环将事件侦听器分配给所有这些对象。
ss2ws0br2#
您可以使用下拉菜单:
第一个
正如许多人提到的,使用内联事件侦听器不是一个好主意。
c9qzyr3d3#
当然。让你的生活更简单,使用一个数据属性(或类似的)和一个通用的按钮结构。你甚至可以从一个数组的东西开始,并生成它的所有...
第一个
bvjxkvbb4#
可以使用
data-(name)
属性和EventListener
第一个
ca1c2owp5#
我可以使用一个纯JavaScript的方法,使用一个
.map()
数组方法和一个Template字符串:当然,在本例中,
'episodesContainer'
字符串将被替换为您希望包含这些按钮的任何元素。希望这对你有帮助。