【发布时间】:2016-10-27 15:11:12
【问题描述】:
我有一个使用 wordpress 作为 cms 构建的网站,并且索引有一个很长的手风琴风格的帖子标题列表,一旦您单击此标题,该帖子的内容就会通过 ajax 加载到索引页面中。
我的问题是,当再次单击标题时,它会再次加载帖子内容,如果您单击不同的标题,它不会关闭并删除已单击的前一个标题。
我是使用 ajax 的新手,不确定如何解决这个问题。有没有办法在第二次单击标题以关闭手风琴或单击另一个标题以展开和打开时删除内容。直播网站在这里:http://www.minervasydney.com/
下面是我的 ajax 代码和我的索引文件中列出标题的部分。如果有人有想法,将不胜感激。谢谢!
AJAX
$("a.exhibitionInformation").on("click", function(event) {
var exhibitionContainer = $(this).parent(".exhibition");
var url = $(this).attr("href");
var doc = $(this).next(".exhibitionDocuments");
var title = convertToSlug($(this).text().split("\n")[1]);
var slug = $(this).attr("data-slug");
$(this).toggleClass("selected");
if($(doc).is(':not(:hidden)')) {
$(doc).slideToggle();
} else {
$.ajax({
url: url,
type: "GET",
success: function(data) {
var content = $(data).find(".single-document");
$(doc).append(content).slideToggle(300);
$("html, body").animate({ scrollTop: exhibitionContainer.offset().top - 26 });
window.location.hash = slug;
}
});
}
event.preventDefault();
});
//ajaxstarStop Loading
$( document ).ajaxStart(function() {
$( "#loading" ).fadeIn(100);
});
$( document ).ajaxStop(function() {
$( "#loading" ).fadeOut();
});
function convertToSlug(Text)
{
return Text
.toLowerCase()
.replace(/ /g,'-')
.replace(/[^\w-]+/g,'')
;
}
索引
<section class="exhibition">
<a class="exhibitionInformation" href="<?php the_permalink(); ?>" data-slug="<?php echo $post->post_name; ?>">
<span class="dates"><?php echo types_render_field("dates", array("argument1"=>"value1")); ?></span>
<span class="opening"><?php echo types_render_field("opening", array("argument1"=>"value1")); ?></span>
<span class="title">“<?php the_title(); ?>”</span>
<span class="artist"><?php echo types_render_field("artist", array("argument1"=>"value1")); ?></span>
<span class="extra"><?php echo types_render_field("extra", array("argument1"=>"value1")); ?></span>
</a>
<article class="exhibitionDocuments">
</article>
</section>
【问题讨论】:
-
在发布答案副本之前测试这个小提琴jsfiddle.net/xogzvsjf/6如果我没记错的话
-
@MamdouhFreelancer 感谢您在小提琴中提出的建议。不幸的是,再次单击标题后,内容仍然翻倍。它似乎也影响了滑动切换,因为单击它时它不会保持打开状态。一旦第二次单击标题,您是否可以考虑另一种方法来删除由 ajax 加载的部分?谢谢!
-
您的意思是如果单击另一个或当前标题,您想中止 ajax 请求吗?测试这个jsfiddle.net/xogzvsjf/10
标签: javascript php jquery ajax wordpress