2012-09-09 91 views
1

我正在寫一個消息系統與jQuery。當你點擊'.open_message'類的線程標題時,它會通過Ajax打開一個包含該線程所有消息的線程。我的問題是,當線程標題被點擊時,它不會識別Firefox和IE中特定線程標題的id屬性。儘管如此,它在Chrome中運行良好。這裏是代碼:單擊事件在Chrome中工作,但在Firefox和IE錯誤

$('.open_message').on('click', function(e) { 
     $(this).parent().removeClass('unread'); 
     $(this).parent().addClass('read'); 
    $('.message_container').html(''); 
    var theID = e.currentTarget.attributes[0].value; 
    theID = theID.replace('#', ''); 
    var url = '".$url."'; 
    var dataString = 'thread_id=' + theID; 
    $('.message_container').append('<img id=\"loading\" src=\"' + url + '/images/loading.gif\" width=\"30px\" />'); 
     $.ajax({ 
      type: 'POST', 
      url: 'get_thread.php', 
      data: dataString, 
      success: function(result) { 
       $('#loading').hide(); 
       $('.message_container').append(result); 
      } 
     }); 
    return false; 
}); 

感謝您的幫助!

+0

你的HTML看起來像什麼?看起來你假設ID是.attributes [0],但爲什麼這是一個很好的假設,因爲.attributes中屬性的順序不是由規範定義的? –

回答

0

在點擊事件中,可以使用this.id訪問元素ID。所以下面應該是一個開始......

$('.open_message').on('click', function(e) { 
    var dataString = 'thread_id=' + this.id, 
     url = '".$url."'; 
    $(this).parent().removeClass('unread').addClass('read'); // Combine for speed 
    $('.message_container').html('').append(etc... 

    etc... 

}); 

我承認你的var url = '".$url."';看起來靠不住給我,但我會假設位工作正常;)

您也不妨使用$.post(如演示here)。

相關問題