简体   繁体   中英

If statement not working in ie7

I'm working on a mousedown function, but for some reason my if statement doesn't work in ie7 within the below code. Works in ie8 up, Chrome and FF.

What am I doing wrong?

$("dd.office").mousedown(function() {
    var btnTxt=$(this).text();
    for (var i = 0; i < offices.length; i++) {
      var teOffice = offices[i];
         if (btnTxt==teOffice[0]){  
           alert("Why Doesnt this work in ie7?");
         }
    }                   
}); 

var offices = [
['Office1', -33.3, 151.426039, 2],
['Office2', -33.9, 151.18743, 3],
['Office3', -37.9, 145.156302, 1]
];

page code

<dl>
<dt>Info</dt>
<dd class="office" >Office1</dd>
<dd class="office" >Office2</dd>
<dd class="office" >Office3</dd>
</dl>

Your btnTxt has a trailing space ( ).

Do one of the following:

  1. btnTxt = $(this).text().replace(/\\s$/,'')
  2. if (btnTxt.replace(/\\s$/,'')==teOffice[0]){

You have a trailing comma inside the offices array. IE chokes on trailing commas.

var offices = [
['Office1', -33.3, 151.426039, 2],
['Office2', -33.9, 151.18743, 3],
['Office3', -37.9, 145.156302, 1],
// ------------------------------^
// remove that.
];
$("dd.office").mousedown(function() {
    var btnTxt=$(this).text();
    for (var i = 0; i < offices.length; i++) {
      var teOffice = offices[i];
         if (btnTxt==teOffice[i]){  
           alert("Why Doesnt this work in ie7?");
         }
    }                   
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM