我希望我的ajax代码应该出现fadeIn效果

时间:2014-02-01 00:11:56

标签: javascript php jquery css ajax

这是我的HTML代码

  <input type="text" name="query_post" id="textid" />

 <input type="button" class="gbutton"  style="-webkit-user-select: none; opacity:1 " id="shareImageButton" value="Share" onclick="Postquery()">

    <div id="new_query_post">  
         </div>

CSS

 .new_query_post{

 display:inline;    

 }

JS

    function Postquery() {


// 1. Create XHR instance - Start
var xhr;
if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
    throw new Error("Ajax is not supported by this browser");
}
// 1. Create XHR instance - End

// 2. Define what to do when XHR feed you the response from the server - Start



xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        if (xhr.status == 200 && xhr.status < 300) {
            document.getElementById('new_query_post').innerHTML = xhr.responseText;
        }

    }
}


// 2. Define what to do when XHR feed you the response from the server - Start

var textid = document.getElementById("textid").value;


// 3. Specify your action, location and Send to the server - Start 
xhr.open('POST', 'postquery.php');
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("textid=" + textid);
// 3. Specify your action, location and Send to the server - End
       }

PHP

<?php 


     $textid =trim($_POST["textid"]);

        echo"
    <div id='each_query'>

        <span style='margin-top:1%; margin-left:3%;float:left;color: #3bb598'>Jan 26'14 </span>
         <span style='margin-top:1%; margin-right:3%;float:right;color: #3bb598'>Hits : 39&nbsp;&nbsp; Views : 60</span>
         <br>
         <table><tr>
         <th><img src='propic/pro_pic.jpg' id='img' align='top'><br><span style='color:#3bb598'>Title</span>  <br><span style='color:#3bb598'><a href='' id='tagid'>Travel</a></span></th>
         <th></th><th></th><th></th>
         <th style='text-align: justify;color: #212121;'>".$textid."  
         </th><th></th><th></th><th></th>   
         </tr></table>
         </div>

                ";

     ?>

我希望新的查询应该以fadeIn效果发布,并且应该在发布之前显示一些ajax加载......

1 个答案:

答案 0 :(得分:1)

jQuery的帖子可能更简单:

var jqXhr = function(e) {
  var $id = $('#textid').val();
  var $spinner = $('#spinner');
  var $result = $('#new_query_post');
  e.preventDefault();
  $result.fadeOut(200);
  $spinner.show();
  $.post('postquery.php', { textid: $id }, function(response) {
    $result.html(response).fadeIn(200);
    $spinner.hide();
  });
}
$('#shareImageButton').on('click', jqXhr);

在HTML中添加<div id="spinner">...</div>(您可以为纯CSS微调器找到一些示例here。)