2016-09-26 72 views
0

我有一個小頁面,我出來和產生的要素之一是測試一些jQuery的:.addClass和.removeClass不點火

<i class="fa fa-arrow-up personstatus" id="status.3140" style="color: #00ff00;" aria-hidden="true"></i>

我使用jQuery做一些修改:

$('.personstatus') 
     .click(function() { 
      var color = $(this).css("color"); 
      var personId = $(this).attr('id').replace('status.', ''); 
      $.get('changestatus/' + personId, 
       function(response) { 
        if (color == 'rgb(0, 255, 0)') { 
         alert('Down'); 
         $('#status.3140').removeClass('fa-arrow-up'); 
         $('#status.3140').addClass('fa-arrow-down'); 
         $('#status.3140').css('color', '#ff0000'); 
        } else { 
         alert('Up'); 
         $('#status.' + personId).removeClass('fa-arrow-down'); 
         $('#status.' + personId).addClass('fa-arrow-up'); 
         $('#status.' + personId).css("color", "#00ff00"); 
        } 
        alert('Finished'); 
       }); 
     }); 

它擊中警戒線爲我所期望的,但它不是應用.removeClass.addClass.css的。

我的jQuery代碼另一部分,在那裏這些優良的工作:

$('.showdetails') 
     .click(function() { 
      var personId = $(this).attr('id'); 
      document.getElementById('currentid').innerHTML = personId; 
      $.get('loadperson/basic/' + personId, 
       function(response) { 
        document.getElementById('persondetails_table').innerHTML = response; 
        $('.tab').removeClass('active'); 
        $('#persondetails_basictab').addClass('active'); 
        $('#person_details') 
         .lightbox_me({ 
          centered: true 
         }); 
       }); 
     }); 

我缺少什麼?

+0

如何有人點擊隱藏的元素? – depperm

+0

@depperm - 它不是一個隱藏的元素,它是一個字體真棒圖標 –

+0

您應該嘗試使用斷點或調試器語句來代替警報。然後,您可以查看那些正在嘗試操作的DOM值,看看它們是否實際可用。我的猜測是你的替換字符串可能是罪魁禍首。 – Swordfish0321

回答

1

ID有問題。您無法將其命名爲「status.3140」。它在這裏更詳細的解釋:CSS selector with period in ID

這是行不通的:

#status.3140 { 
 
    background-color: red; 
 
}
<div id="status.3140">Lorem ipsum</div>

這將工作:

#status\.3140 { 
 
    background-color: red; 
 
}
<div id="status.3140">Lorem ipsum</div>

但是,我強烈建議避免使用ID。最好將其更改爲id="status-3140"

0

感謝@ Swordfish0321,並確認了@Daren Delima和@Barmar,'。'是我的問題。當我通過調試器時,它正在使用'。'解析字符串。作爲一個字符串分離器。我能夠逃脫''。和所有的作品。

感謝您指點我在正確的方向

+0

嗨基思,最好只是標記爲你工作的答案,作爲接受,告訴用戶答案是正確的,離開它,而不是添加另一個答案。 – Celeo