javascript中的全局变量在函数中使用后不会更改

时间:2016-03-15 14:58:57

标签: javascript jquery html css frontend

全局变量var img_id = 2的值;在点击功能中更改。以后它可能会在另一个函数中使用,我希望在按钮运行之前使用click函数时更改该变量的值。但即使在它之前使用了点击功能,它仍然将值显示为2。



function switchVisible() {
            if (document.getElementById('mapButtons')) {

                if (document.getElementById('mapButtons').style.display == 'none') {
                    document.getElementById('mapButtons').style.display = 'block';
                    document.getElementById('modelButtons').style.display = 'none';					
                }
                else {
                    document.getElementById('mapButtons').style.display = 'none';
                    document.getElementById('modelButtons').style.display = 'block';
					
                }
            }
}
function flip() {
    $('.side').toggleClass('rotated');
}

window.img_id = 2;

$(function() {
    $('area').mouseover(function() {
         imgid=$(this).data('imgid');
       // $('#demo').text("Mouseover for "+imgid);
        $('#'+imgid)
            .toggleClass('invert',true)
            .css('z-index',2);
    }).mouseout(function() {
         imgid=$(this).data('imgid');
      //  $('#mi').text("Mouseout for "+imgid);
        $('#'+imgid)
            .toggleClass('invert',false)
            .css('z-index',1);
    }).click(function() {
     imgid=$(this).data('imgid');
    //$('#demo').text("click for "+imgid);

    $('.pressed').removeClass('pressed');
   img_id= imgid;
  //  $('#d').text("new function "+img_id);
    $('#'+imgid)
        .toggleClass('pressed')
        .css('z-index',2);
});
});


$(function() {
 img = img_id;
 $('#d').text("new function "+img_id);
  //var img = 2;
  var min = 2;
  var max = 12;

  $('.prevButton').on('click',function(e) {
    if (img > min) {
       removeAllBorders();
        img--;
        $('#' + img).toggleClass('pressed');
    }
  });

  $('.nextButton').on('click',function(e) {
     if (img < max) {
       removeAllBorders();
       img++;
       $('#' + img).toggleClass('pressed');
     }
  });

  function removeAllBorders() {
     $('img').removeClass('pressed');
  }
});
&#13;
#container img {position: absolute;}

img.invert {
    filter: invert(100%);
    -webkit-filter: invert(100%);
    -moz-filter: invert(100%);
    -o-filter: invert(100%);
    -ms-filter: invert(100%);
}
img.pressed {
    filter: invert(100%);
    -webkit-filter: invert(100%);
    -moz-filter: invert(100%);
    -o-filter: invert(100%);
    -ms-filter: invert(100%);
}

.side {
 position: absolute;
    -webkit-transition:-webkit-transform 0.5s;
    -moz-transition: -moz-transform 0.5s;
    -o-transition: -o-transform 0.5s;
    transition: transform 0.5s;
    -webkit-backface-visibility:hidden;
    -moz-backface-visibility:hidden;
    -ms-backface-visibility:hidden;
    -o-backface-visibility:hidden;
    backface-visibility:hidden;
}
.side.rotated {
    -webkit-transform:rotateY(360deg);
    -moz-transform:rotateY(360deg);
    -ms-transform:rotateY(360deg);
    -o-transform:rotateY(360deg);
    transform:rotateY(360deg);
}
.back {
    -webkit-transform:rotateY(180deg);
    -moz-transform:rotateY(180deg);
    -ms-transform:rotateY(180deg);
    -o-transform:rotateY(180deg);
    transform:rotateY(180deg);
}


#mapButtons { position:absolute;top:90%;}
#modelButtons {
 display: none;
  position:absolute;top:90%;
}
.block {
   width: 100%;
    height: 100%;
   -webkit-perspective:100%;
    -moz-perspective:100%;
    -ms-perspective:100%;
    -o-perspective:100%;
    perspective:100%;   
}


#flip-button {
    background: white;
    font-family:'open sans';
    font-weight: 400;
    color: #5b5b5b;
    border-radius: 5px;
    margin-top: 1em;
}

.prevButton img {
opacity: 1;
-webkit-transition: .3s ease-in-out;
transition: .3s ease-in-out;
}
.prevButton:hover img {
opacity: .5;
}

.nextButton img {
opacity: 1;
-webkit-transition: .3s ease-in-out;
transition: .3s ease-in-out;
}
.nextButton:hover img {
opacity: .3;
}
&#13;
<body>
<map name="map" id="id_1">
<div id="container">
<div class="front side">
<img src="leftovers_ch01.png" style='position:absolute;top:8;left:9;z-index:1'  usemap="#map" >
<img   id="2" class="myBorder" src="http://i.imgur.com/fpqmClk.png" style='z-index:1'  usemap="#map" >
<img   id="3" class="myBorder" src="http://i.imgur.com/TfhyWZs.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="4" class="myBorder" src="http://i.imgur.com/mUKWK5P.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="5" class="myBorder" src="http://i.imgur.com/R1JhAT9.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="6" class="myBorder" src="http://i.imgur.com/yMGFR2V.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="7" class="myBorder" src="http://i.imgur.com/rpCnGxL.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="8" class="myBorder"  src="http://i.imgur.com/FWRCUs4.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="9"  class="myBorder" src="http://i.imgur.com/Aw3NhT9.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="10" class="myBorder" src="http://i.imgur.com/gWhUtyJ.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="11"  class="myBorder" src="http://i.imgur.com/IIJqsxf.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
<img   id="12"  class="myBorder" src="http://i.imgur.com/a9UW5mj.png" style='position:absolute;top:8;left:9; z-index:1'  usemap="#map" >
</div>

   <area  data-imgid="12"  shape="poly" coords="173,223,176,222,183,224,185,217,190,199,193,192,198,187,193,176,158,132,126,92,132,93,123,79,101,97,81,123,66,149,57,170,50,194,48,211,136,217,131,225,149,225,149,221,152,221,156,215,158,208,159,197,167,189,170,179,173,177,169,171,190,176,192,187,190,190,182,198"
   />

   <area  data-imgid="11"  shape="poly" coords="173,229,181,229,184,243,192,256,201,265,201,271,179,309,174,302,125,375,106,361,88,342,71,319,59,295,52,270,48,252,47,243,65,240,61,248,147,234,147,236,150,234,150,238,154,234,154,239,156,235,159,246,165,262,175,275,185,284,195,270,184,259,177,249,174,237,173,229"
   />

   <area  data-imgid="10"  id="ch10" coords="208,268,220,272,234,272,249,266,253,272,235,280,223,280,214,279,215,274,202,283208,298,209,295,222,297,239,296,251,293,261,288,259,293,262,290,262,296,264,294,65,298,266,295,276,310,266,309,304,389,285,398,259,404,241,406,210,407,187,402,169,398,152,390,158,373,162,380,203,273,208,268"   
   />

   <area  data-imgid="9"  class="notbutton" id="ch09" class="hotspot" shape="poly" coords="252,265,255,266,256,271,267,262,275,249,279,233,296,234,291,255,282,270,272,280,268,283,274,284,270,286,274,288,271,290,275,291,272,292,328,359,319,359,331,374,349,360,366,342,381,321,392,299,400,276,404,257,407,240,318,235,323,227,278,227,272,231,269,244,262,256,252,265" 
   />

   <area  data-imgid="8"  class="notbutton" id="ch08" class="hotspot" shape="poly" coords="251,188,253,181,267,192,273,200,277,208,278,212,274,213,288,220,298,207,294,207,289,196,284,186,274,174,262,166,268,166,266,164,270,162,267,161,270,159,266,158,276,142,279,151,329,78,338,85,349,94,363,108,379,129,391,152,397,168,403,189,406,211,389,213,393,206,277,225,272,222,269,209,262,197,251,188" 
   />

   <area  data-imgid="7" class="notbutton" id="ch07" class="hotspot" shape="poly" coords="247,186,248,182,251,179,239,175,228,173,216,174,206,177,199,161,213,157,225,156,239,157,254,162,254,158,256,159,257,155,258,157,259,152,260,154,292,72,296,79,302,63,286,56,263,50,242,47,213,47,188,51,166,57,149,64,188,143,179,143,201,181,208,185,218,182,236,182,247,186" 
   />

   <area  data-imgid="6" class="notbutton" id="ch06_09" shape="poly" coords="156,234,156,226,154,222,154,226,150,223,150,226,147,224,148,227,130,227,135,220,47,213,31,223,16,212,15,226,16,236,16,243,33,254,48,243,66,240,61,248,148,233,147,236,151,234,151,237,153,234,154,239,156,234"   
   />

  <area  data-imgid="6"  class="notbutton" id="ch06_08" shape="poly" coords="173,230,173,227,180,224,184,228,180,230,173,230"   
  />   

  <area  data-imgid="6"  class="notbutton" id="ch06_07" shape="poly" coords="205,264,207,268,203,272,162,380,158,372,151,389,154,407,136,417,124,412,115,406,108,401,125,394,125,375,174,302,179,309,200,271,201,266,205,264"   
  />  

  <area  data-imgid="6"  class="notbutton" id="ch06_06" shape="poly" coords="261,288,260,291,263,290,262,295,264,293,264,297,266,295,275,309,266,309,305,389,321,397,319,415,330,412,341,405,347,400,347,379,330,374,318,359,328,359,272,292,274,291,271,289,275,288,269,287,273,284,267,284,261,288"   
   /> 

   <area  data-imgid="6" class="notbutton" id="ch06_05" shape="poly" coords="249,264,249,268,253,273,256,271,254,266,249,264"   
  /> 

  <area  data-imgid="6"  class="notbutton"id="ch06_04" shape="poly" coords="271,227,273,223,277,225,393,206,388,214,407,211,420,200,436,210,438,218,438,229,437,242,422,231,406,240,318,235,323,227,277,227,274,230,271,227"   
   /> 

   <area  data-imgid="6" class="notbutton" id="ch06_03" shape="poly" coords="256,163,262,166,267,165,265,164,269,162,266,161,269,159,266,158,276,142,279,150,329,78,329,59,344,51,333,45,323,39,316,36,300,45,303,63,296,80,292,72,260,154,259,153,259,158,256,155,257,159,254,158,256,163"   
   /> 

   <area  data-imgid="6" class="notbutton" id="ch06_02" shape="poly" coords="248,189,247,183,251,180,253,181,252,186,248,189"   
   /> 

   <area  data-imgid="6" class="notbutton" id="ch06_01" shape="poly" coords="199,191,203,188,207,192,205,187,207,185,202,182,178,142,188,143,149,64,133,55,133,37,122,44,112,49,107,53,107,73,123,78,135,93,126,93,199,183,199,191"   
   /> 

   <area  data-imgid="5" class="notbutton" id="ch05_06" shape="poly" coords="16,212,31,223,47,214,50,189,55,169,62,152,70,136,81,120,92,107,105,93,118,82,123,79,107,73,107,54,91,65,73,83,57,101,45,119,36,137,27,158,20,183,17,202,16,212"   
   /> 

   <area  data-imgid="5" class="notbutton"id="ch05_05" shape="poly" coords="108,401,125,394,125,375,112,366,97,352,83,336,69,316,61,298,54,280,49,261,47,243,33,255,16,244,20,270,27,293,34,313,45,334,57,352,71,369,88,385,108,401"   
   /> 

   <area  data-imgid="5" class="notbutton" id="ch05_04" shape="poly" coords="305,389,321,398,319,415,290,427,265,433,237,436,208,436,181,432,159,426,136,417,154,407,151,391,171,399,192,404,213,407,236,407,261,404,280,399,296,393,305,389"   
   /> 

    <area  data-imgid="5" class="notbutton" id="ch05_03" shape="poly" coords="406,240,423,231,437,242,434,264,429,285,424,302,416,320,404,341,390,360,375,376,357,391,347,399,347,380,331,374,345,363,358,350,370,336,380,323,387,309,395,294,399,279,403,265,406,247,406,240"   
   /> 

    <area  data-imgid="5" class="notbutton"id="ch05_02" shape="poly" coords="329,77,328,58,344,52,363,66,383,85,396,101,410,123,421,144,428,164,433,182,436,199,436,210,420,200,406,211,404,191,398,169,390,149,380,131,365,110,352,97,339,86,329,77"   
   /> 

    <area  data-imgid="5" class="notbutton" id="ch05_01" shape="poly" coords="132,55,134,37,152,30,174,23,200,18,219,17,240,17,260,19,276,22,291,26,303,31,316,36,300,45,303,63,284,55,265,51,250,48,234,46,215,47,194,49,175,53,157,60,149,64,132,55"   
  /> 

   <area  data-imgid="5" class="notbutton" id="ch04_03" shape="poly" coords="195,270,185,284,174,273,166,263,160,251,157,240,156,226,158,211,163,195,174,180,177,177,174,175,191,176,192,191,188,189,178,203,173,219,173,233,177,248,185,261,195,270"   
   /> 

    <area  data-imgid="4" class="notbutton" id="ch04_02" shape="poly" coords="202,283,215,275,214,279,223,280,234,280,245,277,257,271,265,264,273,253,278,241,280,233,296,235,290,257,283,269,276,276,267,285,251,292,235,297,220,297,210,295,209,298,202,283"   
  /> 

    <area  data-imgid="4" class="notbutton" id="ch04_01" shape="poly" coords="206,177,216,174,231,173,247,177,257,183,266,191,273,201,278,212,274,213,288,220,297,207,294,208,289,196,282,184,271,172,260,165,245,159,230,156,214,157,199,161,206,177"   
   /> 

   <area  data-imgid="2" id="ch02" shape="poly" coords="214,234,216,246,228,241,238,246,240,233,248,227,239,219,237,207,226,212,216,208,215,219,205,227,214,234"   
   /> 

    <area  data-imgid="3" id="ch03" shape="poly" coords="181,226,189,227,181,229,182,237,186,247,192,256,199,262,203,266,207,261,206,267,213,270,222,272,232,272,242,269,250,266,247,262,251,266,259,259,265,251,269,242,272,234,272,227,267,227,272,226,271,218,269,209,264,201,257,193,250,188,247,191,247,187,239,182,226,181,214,183,207,186,205,187,208,194,203,188,196,193,189,201,184,211,181,221,181,226"   
   /> 
 </map>

<div class="back side">
<img id="image1" src="blbl.png" style='position:absolute;top:8;left:9;' usemap="#map2" />
</div>
</div>
<p id="demo"></p>
<p id="mi"></p>
<p id="clicked"></p>
<div id = "mapButtons" >
<button class = "prevButton" type="button"><img id="image" src="http://i.imgur.com/PjVnMY1.png"></button>
<button id="flip-button" class = "changeButton" type="button" onclick= "flip();switchVisible();"><img id="image" src="mapButton1.png"></button>
<button class = "nextButton" type="button"><img id="image" src="http://i.imgur.com/sR3xfJl.png"></button>
</div> 

<div id = "modelButtons" >
<button class = "prevButton" type="button"><img id="image" src="prevButton.png"></button>
<button id="flip-button" class = "changeButton" type="button" onclick= "flip(); switchVisible();"><img id="image" src="modelButton1.png"></button>
<button class = "nextButton" type="button" ><img id="image" src="nextButton.png"></button>
</div>

 <p id="d"></p>
</body>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

要使用全局变量,首先必须使用var example通知此变量。

但是,要在函数中使用它,你不能像你那样重新声明它,因为它只在本地函数中宣布该变量。

要在本地函数中使用和编辑全局变量,只需传递example = 5;,而无需使用&#34; var&#34;来重新声明它。关键词。它必须宣布的唯一时间是在全球范围内

这是fiddle。 正如您所看到的,我在开始时初始化了变量(并删除了&#39; window.img_id&#39;),它似乎完全正常工作。你是如何检查变量的?

答案 1 :(得分:1)

删除var img = img_id;并直接在按钮单击侦听器中使用img_id。在加载脚本时将img分配给img_id的值,并从img和img_id那里获取单独的生命......

答案 2 :(得分:1)

问题或原因:
第二个function()块在主功能块内部执行img = img_id,执行一次,然后在初始页面加载后再也不执行。它还定义了一些事件处理程序,但它们都使用img的值,不再更改 ...只有事件处理程序被多次调用,而不是它们之前的代码

<强>解决方案:
直接在这些事件处理程序中使用img_id