简体   繁体   English

自动刷新div中的内容?

[英]Auto-refresh content inside a div?

I know this question has been asked several times. 我知道这个问题已经问过几次了。 And I have read all the threads without coming to a solution. 我已经阅读了所有线程,而没有找到解决方案。 I use Wordpress, together with a plugin, this plugin lets me add events to it. 我使用Wordpress以及一个插件,该插件使我可以向其中添加事件。 I use the "shortcode"/[slug] on a Wordpress page. 我在Wordpress页面上使用“简码” / [子词]。 This displays the events I have added. 这将显示我添加的事件。 If I add or remove an event I need to manually refresh the page to see this. 如果添加或删除事件,则需要手动刷新页面才能看到此事件。

My goal is to be able to see the new events (and removed) without refreshing the whole page. 我的目标是在不刷新整个页面的情况下能够查看新事件(并删除)。 If I check the page with chrome together with the source I find that a div id="vs" is what contains all the events. 如果我用chrome和源代码一起检查页面,我会发现div id =“ vs”包含所有事件。 I have made an addition to the plugin where I've created a javascript. 我在创建JavaScript的插件上做了一个补充。

jQuery(document).ready( function($) {

var auto_refresh = setInterval(
function() {
$.ajax({
    url: "https://example.com",
    success: function( data ) {
        $("#vs").html( $(data).find('div').html());
    }
})
}, 5000); // refreshing after every 5000 milliseconds
})

What happens is that it is refreshed automatically. 发生的是它会自动刷新。 But it displays a lot of elements, and it is a duplicate of the content that was already there. 但是它显示了很多元素,并且是已经存在的内容的重复。 And everything is offset too. 一切也都抵消了。

I really don't know what to try? 我真的不知道该怎么办? And also, the page which this is displayed on is a slider, both displaying the same slug, but it only "works" on the first slider. 而且,此页面上显示的页面是一个滑块,两个页面都显示相同的子控件,但仅在第一个滑块上“起作用”。

Any ideas? 有任何想法吗?

So it's a little hard to answer without seeing what all the dom looks like but it sounds like you are not replacing the content but rather just adding to it over and over again. 因此,如果不了解所有dom的外观,很难回答,但听起来好像您不是在替换内容,而是一次又一次地添加内容。 As a result, slowly the width content is getting smaller and smaller which is why you get the indenting affect. 结果,宽度内容逐渐变得越来越小,这就是为什么要产生缩进影响的原因。 My guess is you're using .html when you want to be using .replaceWith http://api.jquery.com/replacewith/ 我的猜测是,当您要使用.replaceWith http://api.jquery.com/replacewith/时,您正在使用.html。

var auto_refresh = setInterval(
function() {
$.ajax({
    url: "https://example.com",
    success: function( data ) {
        $("#vs").replaceWith( $(data).find('div').html());
    }
})
}, 5000); // refreshing after every 5000 milliseconds

If this doesn't work I think we need to see what your dom looks like. 如果这不起作用,我认为我们需要查看您的dom的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM