簡體   English   中英

我究竟做錯了什么? 沒有錯誤

[英]what am I doing wrong? No errors

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); 
}
      }
    });
}

味精,我得到的響應:(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>

問題

您的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

解決方案

您應該使用以下命令將文本直接添加到else的原始div中:

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 ,不需要#showWall... .hide() #showWall... div,因為div由於style="display: none;"而在div上是不可見的style="display: none;"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM