【问题标题】:wordpress jQuery ajax loads unwanted duplicate postswordpress jQuery ajax 加载不需要的重复帖子
【发布时间】:2013-07-01 16:59:38
【问题描述】:

我会尽力解释。基本上我有一个项目网格(砌体),我想在每个网格 div(红色)内附加 ajax 加载的内容(wordpress single.php 帖子)。

我在填充内容的每个红色 div 中都有一个名为 ajaxcontainer 的 div 类。当我单击 a href 触发器内容时,它应该被附加,这工作一次,但是当我单击网格中的另一个项目时,旧的 ajaxcontainer 会填充来自该 href 的新内容。基本上是重复的。

我希望旧的 ajaxcontainer 保留旧内容,即使我单击了另一个项目。

HTML

<article style="background:<?php echo $color ?>;" id="post-<?php the_ID(); ?>" 
<?php post_class($classes); ?>>


<div class="hover">
<h2><?php echo $head ?></h2>
<p class="tags"><?php echo $tagsstring; ?></p>

<a href="//<?php echo $url ?>"><?php echo $url ?></a>
<a class="trick" rel="<?php the_permalink(); ?>" href="<?php the_permalink();?>">goto</a>
</div>

<div class="thumbnail <?php echo $paddingstring?>" style="background-image:url(<?php echo $thumbnail[0] ?>);">

</div>


</article><!-- #post-## -->

我现在拥有的:

   $.ajaxSetup({
       cache: false,
       success: function (result, status, xhr) {
           // not showing the alert
           console.log('success');
           var $this = jQuery(this)
           $('.ajaxcontainer', $this).hide().fadeIn();
       },
       beforeSend: function () {
           console.log('beforesend');
           $(".ajaxcontainer").html("loading...");


       },
       complete: function (xhr, stat) {
           // hide dialog // works

       }
   });
   $(".trick").each(function () {
       $(this).on("click", function (e) {
           $(this).parents('.item').append("<div class='ajaxcontainer'>hello world</div>")

           var post_link = $(this).attr("href");
           $(".ajaxcontainer").load(post_link + ' #content');
           return false;
       });
   });

任何帮助表示赞赏:)

【问题讨论】:

  • 编辑后添加的html
  • 类“item”的父div在哪里
  • 试试这个小提琴jsfiddle.net/arunu/W5nyJ/3
  • 类 .item 在文章 div 中,由 wordpress 生成。小提琴非常接近,但现在我不知道如何让它再次加载帖子
  • 你可以在js fiddle中添加你的页面源html并尝试

标签: php jquery ajax html


【解决方案1】:

我可能错了,但据我所知,如果您有重复的内容,next() 可能会有所帮助。

函数中类似于 $(this).parents('.item').next().append("hello world")。

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多