javascript新手想要压缩一些代码

时间:2014-01-19 20:21:23

标签: javascript php compression

我对PHP有一些乐趣,然后我意识到我真的需要使用JavaScript。所以我做了一些研究,找到了我需要的东西 - 它可以正常工作。

但是有一点问题就是这段代码看起来很糟糕......我对JavaScript的所有了解都是alert("something");的作用类似于echoprint。但语法类似于PHP,所以我稍后可以对你的提示进行一些研究。此外,我不需要任何关于以下代码的安全提示,我已经处理好了。

代码:

    <script type="text/javascript">
function getCook(cookiename) 
  {
  var cookiestring=RegExp(""+cookiename+"[^;]+").exec(document.cookie);
  return unescape(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./,"") : "");
  }

function UpdateBet()
{
var cookieValue = getCook('bet');
document.getElementById("bet0").href="?bet=0&amount=" + cookieValue;
document.getElementById("bet1").href="?bet=1&amount=" + cookieValue;
document.getElementById("bet2").href="?bet=2&amount=" + cookieValue;
document.getElementById("bet3").href="?bet=3&amount=" + cookieValue;
document.getElementById("bet4").href="?bet=4&amount=" + cookieValue;
document.getElementById("bet5").href="?bet=5&amount=" + cookieValue;
document.getElementById("bet6").href="?bet=6&amount=" + cookieValue;
document.getElementById("bet7").href="?bet=7&amount=" + cookieValue;
document.getElementById("bet8").href="?bet=8&amount=" + cookieValue;
document.getElementById("bet9").href="?bet=9&amount=" + cookieValue;
document.getElementById("bet10").href="?bet10&amount=" + cookieValue;
document.getElementById("bet11").href="?bet=11&amount=" + cookieValue;
document.getElementById("bet12").href="?bet=12&amount=" + cookieValue;
document.getElementById("bet13").href="?bet=13&amount=" + cookieValue;
document.getElementById("bet14").href="?bet=14&amount=" + cookieValue;
document.getElementById("bet15").href="?bet=15&amount=" + cookieValue;
document.getElementById("bet16").href="?bet=16&amount=" + cookieValue;
document.getElementById("bet17").href="?bet=17&amount=" + cookieValue;
document.getElementById("bet18").href="?bet=18&amount=" + cookieValue;
document.getElementById("bet19").href="?bet=19&amount=" + cookieValue;
document.getElementById("bet20").href="?bet=20&amount=" + cookieValue;
document.getElementById("bet21").href="?bet=21&amount=" + cookieValue;
document.getElementById("bet22").href="?bet=22&amount=" + cookieValue;
document.getElementById("bet23").href="?bet=23&amount=" + cookieValue;
document.getElementById("bet24").href="?bet=24&amount=" + cookieValue;
document.getElementById("bet25").href="?bet=25&amount=" + cookieValue;
document.getElementById("bet26").href="?bet=26&amount=" + cookieValue;
document.getElementById("bet27").href="?bet=27&amount=" + cookieValue;
document.getElementById("bet28").href="?bet=28&amount=" + cookieValue;
document.getElementById("bet29").href="?bet=29&amount=" + cookieValue;
document.getElementById("bet30").href="?bet=30&amount=" + cookieValue;
document.getElementById("bet31").href="?bet=31&amount=" + cookieValue;
document.getElementById("bet32").href="?bet=32&amount=" + cookieValue;
document.getElementById("bet33").href="?bet=33&amount=" + cookieValue;
document.getElementById("bet34").href="?bet=34&amount=" + cookieValue;
document.getElementById("bet35").href="?bet=35&amount=" + cookieValue;
document.getElementById("bet36").href="?bet=36&amount=" + cookieValue;
document.getElementById("poziom1").href="?bet=poziom1&amount=" + cookieValue;
document.getElementById("poziom2").href="?bet=poziom2&amount=" + cookieValue;
document.getElementById("poziom3").href="?bet=poziom3&amount=" + cookieValue;
document.getElementById("bloczek1").href="?bet=bloczek1&amount=" + cookieValue;
document.getElementById("bloczek2").href="?bet=bloczek2&amount=" + cookieValue;
document.getElementById("bloczek3").href="?bet=bloczek3&amount=" + cookieValue;
document.getElementById("ODD").href="?bet=ODD&amount=" + cookieValue;
document.getElementById("EVEN").href="?bet=EVEN&amount=" + cookieValue;
document.getElementById("RED").href="?bet=RED&amount=" + cookieValue;
document.getElementById("BLACK").href="?bet=BLACK&amount=" + cookieValue;
document.getElementById("1-18").href="?bet=1-18&amount=" + cookieValue;
document.getElementById("18-36").href="?bet=18-36&amount=" + cookieValue;
}
</script>

1 个答案:

答案 0 :(得分:0)

根据评论的建议,使用for循环,您可以尝试类似:

示例:

<script type="text/javascript">
//get your cookie, you know what to do
var cookieValue = 'test';

function UpdateBets(){
    var bets = document.getElementsByClassName("bet");
    for (i in bets){
        bets[i].href += cookieValue;
    }
}
</script>

<a href="?bet=0&amount=" class="bet">Bet A</a>
<a href="?bet=1&amount=" class="bet">Bet B</a>
<a href="?bet=2&amount=" class="bet">Bet C</a>
<a href="?bet=3&amount=" class="bet">Bet D</a>
...
...

<script>UpdateBets();</script>