gpt4 book ai didi

javascript - 帮助在 Wordpress 循环中使用一些 jQuery

转载 作者:行者123 更新时间:2023-11-29 22:39:52 24 4
gpt4 key购买 nike

我正在制作一个自定义存档页面,它将显示带有摘录的帖子列表,以及一个按钮,单击该按钮可切换其余帖子内容。我让它与这段代码一起工作,除了它会切换每个帖子内容大声笑因为在循环中它们都得到了类你知道我的意思吗?

你能帮我找到一个方法来做到这一点吗?必须有办法让它在循环中工作吗?这是我到目前为止的(失败)代码,谢谢你!`

<div id="more_content">
<a href="#" class="see_all">More</a>
<div class="content_hidden" style="display:none;">
<div class="copy">
<?php the_content(); ?>
</div>

<p class="tags"><?php the_tags(); ?></p>
<?php comments_template(); ?>
</div>
</div>

<script type="text/javascript" charset="utf-8">

$('.see_all').click(function() {

if ( $(this).html() == 'More'){
$('.content_hidden').toggle('slow');

$(this).html('Less');



}
else if ( $(this).html() == 'Less'){

$('.content_hidden').slideUp('slow');
$(this).html('More');

}

return false;

});

`

最佳答案

这应该有效:

$('.see_all').click(function() {
if ( $(this).html() == 'More'){
$(this).parent().find('.content_hidden').toggle('slow');
$(this).html('Less');
} else if ( $(this).html() == 'Less'){
$(this).parent().find('.content_hidden').slideUp('slow');
$(this).html('More');
}
return false;
});

引用你的话:

it toggles EVERY post content lol because in the loop they all get the class you know what I mean

您需要做的是解决 .content-hidden点击链接的每个容器内或相对于这些容器,而不是同时处理所有这些容器。此外,您可能会在文档中重复 ID(这是不好的、禁止的、禁忌的、不要做的、无效的、违反规范的、不起作用的,等等)。所以,你的 <div id="more_content">也许应该改为 <div class="more_content">

关于javascript - 帮助在 Wordpress 循环中使用一些 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3482053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com