Что я делаю неправильно?Нет ошибок - PullRequest
0 голосов
/ 17 сентября 2010
function showComments(wallID){
    $.ajax({
      url: "misc/showComments.php",
             type: "POST",
      data: { mode: 'ajax', wallID: wallID }, 
      success: function(msg){

      var $msg = $('#showWallCommentsFor'+wallID).find('.userWallComment');
// if it already has a comment, fade it out, add the text, then toggle it back in
if ( $msg.text().length ) {
  $msg.fadeOut('fast', function(){
    $msg.text( msg ).slideToggle(300); 
  });
} else {
  // otherwise just hide it, add the text, and then  toggle it in
  $msg.hide().text( msg ).slideToggle(300); 
}
      }
    });
}

msg, ответ, который я получаю: (firebug)

    <span class='userWallComment'>
<span style='float: left;'>
<img style='border: 1px solid #ccc; width: 44px; height: 48px; margin-right: 8px;' src='images/profilePhoto/thumbs/noPhoto_thumb.jpg'>
</span></span>
<span style='font-size: 10px; margin-bottom: 2px;'>
<a href='profil.php?id=1'>Navn navn</a> - igår kl. 01:55
</span>
<br>
DETTE ER EN TEST
<br>
<div class="clearfloat"></div>
</span>

Он правильно отправляет и выполняет вызов ajax, и у него есть что-то в ответе, но он не переключает его?

Это div:

<div id="showWallCommentsFor<?php echo $displayWall["id"]; ?>" style="display: none;">
</div>

1 Ответ

1 голос
/ 17 сентября 2010

Проблема

Ваш оператор if - else имеет недостаток:

if ( $msg.text().length ) {
  //  ...
} else {
  // $msg has a length of ZERO by definition here!!!
  $msg.hide().text( msg ).slideToggle(300); 
}

При первом вызове AJAX #showWallCommentsFor пусто, поэтому в нем нет .userWallComment, поэтому $msg не будет определено.

Решение

Вы должны добавить текст непосредственно к исходному div в вашем else, используя:

if ( $msg.text().length ) {
  //  ...
} else {
    // otherwise just hide it, add the text, and then  toggle it in
      // You cannot use $msg here, since it has a length of 0.
      // Add text directly to the original div instead.
      // You do not need to hide the DIV first since it is already 
      // invisible.
    $('#showWallCommentsFor'+wallID).text( msg ).slideToggle(300); 
 }

Наконец, в вашем else нет необходимости .hide() div #showWall..., так как div изначально невидим из-за style="display: none;".

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...