jQuery的:remplace内容只有当新的内容是不同的 [英] Jquery : remplace content only if the new content is different

查看:74
本文介绍了jQuery的:remplace内容只有当新的内容是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程一个网站,但我有一个小问题...

I'm programming a website but i have a little problem...

我有被称为每隔X秒的AJAX刷新内容的PHP文件。这个PHP文件返回一个js文件至极必须改变div的HTML内容与效果...

I have a php file which is called every X second in ajax to refresh a content. This php file return a js file wich must change the html content of a div with a effect...

但我想改变这个内容只有当新的不一样!否则,我有一个div它刷新自己的作用,即使是萨姆斯内容!

But i would want to change this content only if the new is different! Else, i have a div which refresh itself with the effect even if it's the sames contents!

我这个code测试:

if ($('#mydiv').html()!=data_insertion) $('#mydiv').fadeTo("fast", 0, function () {$(this).html(data_insertion).fadeTo("fast", 1)});

但是,这code不是跨浏览器......事实上,我data_insertion变量包含一些HTML内容。而每个浏览器插入改变attribut顺序等等。这就是为什么$('#mydiv)。HTML()永远不会等于data_insertion ...

But this code isn't cross browser... Indeed, my data_insertion variable contains some html content. And each browser insert it changing the attribut order etc. That's why, $('#mydiv').html() is never equal to data_insertion...

有一个答案好吗?

非常感谢!

推荐答案

您可以使用结果的本地缓存,连接到该元素。 在您的Ajax回调做这样的事情:

You can use a local cache of the result, attached to the element. In your ajax callback do something like:

if (jQuery.data($('#mydiv')[0], 'last_data_insertion') != data_insertion) {
    // Update DOM content
    $('#mydiv').fadeTo("fast", 0, function () {$(this).html(data_insertion).fadeTo("fast", 1)});

    // Reset the local cache
    jQuery.data($('#mydiv')[0], 'last_data_insertion', data_insertion);
}

这篇关于jQuery的:remplace内容只有当新的内容是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆