如果声明不在ie7中工作

时间:2011-08-22 01:13:24

标签: javascript jquery internet-explorer-7

我正在处理mousedown函数,但由于某种原因,我的if语句在以下代码中的ie7中不起作用。适用于ie8 up,Chrome和FF。

我做错了什么?

$("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]
];

页码

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

3 个答案:

答案 0 :(得分:2)

您的btnTxt有一个尾随空格()。

执行以下操作之一:

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

答案 1 :(得分:1)

offices数组中有一个尾随逗号。 IE在尾随逗号上窒息。

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

答案 2 :(得分:0)

$("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?");
         }
    }                   
});