1

我有这个代码:

<div id="quote">
  <p>
    Click on image please.
  </p>
</div>    
<img class="post" src="positif.png" title="rate this positive" onclick="positif(this);">

如果单击图像,它将调用此函数:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
   var jq = jQuery.noConflict(true);
</script>
<script type="text/javascript">
  function positif(obj) {
    var url = obj.parentNode.valueOf('href');
    var nama = obj.parentNode.innerText;
    alert(url);
    jq("#quote p").load("retrain/pos.php?url=" + url + "&nama=" + nama);
  }    
</script>

这个load东西叫这个pos.php脚本:

$url = $_GET["url"];
$nama = $_GET["nama"];
$nama2 = str_replace(str_split('\\/:*?"<>|()'), '', $nama);
$post = '../cat/statistika/S_' . $nama2 . '.txt';

    $current = file_get_contents($url);
    if (file_get_contents($url)) {
        $current2 = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $current);
        $current3 = preg_replace('#<style(.*?)>(.*?)</style>#is', '', $current2);
        $current4 = strip_tags($current3);
        $current5 = preg_replace('/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', '', $current4);
        file_put_contents($post, $current5);
        echo "Rated positive, Thanks for your response!";
    } else {
        echo 'Sorry, connection failed. Try again later.';
    }

他们工作正常。但是由于我file_put_content在那里使用,它需要时间,并且在成功时它不会显示任何“变化” put_content。通过扩展,我必须在 PHP(或 javascript?)工作时创建进度条或加载图像等最终显示Rated positive blah2.

我把它放在positif(obj)函数的末尾:

jq('body').ajaxStart(function() {
jq('#loading').show();
}).ajaxComplete(function() {
jq('#loading').hide();
});

除了div上面的另一个div#quote p,还有这个:

<div id="loading">
  <img id="loading-image" src="loading_thing.gif" alt="Loading..." style="display: none"/>
</div>

没用。我在这里没有做什么?

4

3 回答 3

0

From ajaxStart doc:

As of jQuery 1.8, the .ajaxStart() method should only be attached to document.

So try:

jq('document').ajaxStart(function() {
  jq('#loading, #loading-image').show();
}).ajaxComplete(function() {
  jq('#loading, #loading-image').hide();
});
于 2013-09-09T03:22:12.263 回答
0

don't forget to show your image placeholder as well

jq('body').ajaxStart(function() {
    jq('#loading, #loading-image').show();
}).ajaxComplete(function() {
    jq('#loading, #loading-image').hide();
});
于 2013-09-09T03:22:22.723 回答
0

我想如果你把它放在 line 之后alert(url);,它会更好地工作:

jq('#loading').fadeIn(); // Just esthetic, you could use .show()
jq("#quote p").load("retrain/pos.php?url=" + url + "&nama=" + nama,
function () {
     jq('#loading').fadeOut(); // Just esthetic, you could use .hide()
});

希望能帮到你。

于 2013-09-09T03:28:53.580 回答