Javascript语句符合所有可能出错的条件

时间:2014-04-03 18:55:15

标签: javascript jquery

此代码符合所有条件,有人请告诉我哪里出错了;我检查过,我看不出代码有什么问题。为什么满足所有条件。

<script>
$( document ).ready(function() {
    var style = $('#el_teams_TeamFormation span').html(); 
    alert (style);
         if (style = "4-4-2")
          { 
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch1').style.display = "block";
         }
         if (style = "3-4-3")
          {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch2').style.display = "block";
         } 
         if (style = "3-5-2") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch3').style.display = "block";
         }
         if (style = "4-5-1") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch4').style.display = "block";
         }
});

2 个答案:

答案 0 :(得分:3)

值得熟悉不同类型的比较运算符:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

简单地说,单个(=),双(==)和三个(===)等于符号,每个符号都表示不同的JavaScript。

单个=是赋值运算符,因此在每个if语句中,您实际上都要求脚本为变量{赋值“4-4-2”(依此类推) {1}}而不是比较这两个值。

如果您使用style(带有类型强制)或==(没有强制),您的代码应该可以正常使用。

答案 1 :(得分:1)

if (style == "4-4-2")
          { 
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch1').style.display = "block";
         }
         if (style == "3-4-3")
          {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch2').style.display = "block";
         } 
         if (style == "3-5-2") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch3').style.display = "block";
         }
         if (style == "4-5-1") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch4').style.display = "block";
         }