我对Javascript很新,并试图复制我在电子表格上做的事情,但是在网页上的javascript中,我已经让它以漫长的方式工作,寻找简化它的方法。
该脚本嵌入在HTML
中<td class="td_Col11cSize">
<div class="TableText">
<span class = "Sub0a">
<script type="text/javascript">
if (Game2Player4Sub == "")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
<span class = "Sub1a">
<script type="text/javascript">
if (Game2Player4Sub == "Sub 1")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
<span class = "Sub2a">
<script type="text/javascript">
if (Game2Player4Sub == "Sub 2")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
<span class = "Sub3a">
<script type="text/javascript">
if (Game2Player4Sub == "Sub 3")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
<span class = "Sub4a">
<script type="text/javascript">
if (Game2Player4Sub == "Sub 4")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
<span class = "Sub5a">
<script type="text/javascript">
if (Game2Player4Sub == "Sub 5")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
<span class = "Sub6a">
<script type="text/javascript">
if (Game2Player4Sub == "Sub 6")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
<span class = "Sub7a">
<script type="text/javascript">
if (Game2Player4Sub == "Sub 7")
document.write(Game2Player4Name, Game2Player4Off)
</script>
</span>
</div>
</td>
代码检查8个条件以设置字体颜色,并在括号中添加玩家的名字和时间。上面的工作,但很长,需要看一下写一个函数来替换它在页面上使用18 x 46次。
我还需要在<img src ="YellowCard.png">
(Game2Player4Yellow == "n")
答案 0 :(得分:0)
你正在使用JS,好像它是PHP ......
尝试类似:
function getGame2PlayerData(Game2Player4Sub, Game2Player4Yellow = null) {
selector = Game2Player4Sub.replace(" ","") + 'a';
elements = getElementsByClassName(selector);
img = '';
if (Game2Player4Yellow == 'n') {
img = '<img src ="YellowCard.png">';
}
elements[0].innerHTML = img + Game2Player4Name + ' ' + Game2Player4Off;
}