首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏和显示多个div onclick jQuery - WordPress

隐藏和显示多个div onclick jQuery - WordPress
EN

Stack Overflow用户
提问于 2019-06-05 19:01:10
回答 2查看 221关注 0票数 0

使用jQuery,我想在单击时隐藏和显示多个div。我写了一些代码,但它不工作。

我的JavaScript只有在我把所有的div都设置到一个新的行时才能工作。但是我想一次选择多个div,这样代码会更短。我尝试使用document.querySelectorAll一次选择多个div。但这也没有起到作用。因此,现在我尝试使用jQuery。

到目前为止,这是我的代码。但它不起作用。

代码语言:javascript
复制
$(document).ready(function(){
  $("#fm-button").click(function(){
    $("#first-image, #subtitle, #content").show();
    $("#content-1, #content-2, #subtitle-1, #subtitle-2, #second-image, #third-image").hide();
  });
});

这是HTML。我也在使用PHP。

代码语言:javascript
复制
<?php if(get_sub_field('FM_button_text') ) : ?>
    <div class="FM-button" id="fm-button" onclick="screenChangeFm()">
        <img class="FM-card-image" src="<?php the_sub_field('FM_image'); ?>" alt="feedmanagement_image">
        <p class="card-text"><span><?php the_sub_field("FM_button_text"); ?></span></p>
    </div>
<?php endif; ?>

<h2 class="sub" id="subtitle">Some Text 0</h2>
<h2 class="sub" id="subtitle-1" style="display: none">Some Text 1</h2>
<h2 class="sub" id="subtitle-2" style="display: none">Some Text 2</h2>

<img id="first-image" src="<?php the_sub_field('image-1'); ?>"alt="helpcenter_image">
<img id="second-image" src="<?php the_sub_field('image-2'); ?>" alt="helpcenter_image" style="display: none">
<img id="third-image" src="<?php the_sub_field('image-3'); ?>" alt="helpcenter_image" style="display: none">

<p id="content"><?php the_sub_field('content'); ?></p>
<p id="content-1" style="display: none">Subtext!</p>
<p id="content-2" style="display: none">Subtext! $#2</p>

I expect that when I click on #fm-button, some divs will hide and other will show.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-05 19:49:06

这是向HTML对象添加侦听器的另一种方式。

代码语言:javascript
复制
$(document).on('click',"#fm-button",function(event){
    $("#first-image, #subtitle, #content").show();
    $("#content-1, #content-2, #subtitle-1, #subtitle-2, #second-image, #third-image").hide();
});

告诉我它能不能用。

票数 0
EN

Stack Overflow用户

发布于 2019-06-05 20:49:50

@wasanga7我使用的是WordPress,而不是$,我需要使用jQuery。代码现在可以工作了!

代码语言:javascript
复制
jQuery(document).on('click',"#fm-button",function(event){
    jQuery("#first-image, #subtitle, #content").show();
    jQuery("#content-1, #content-2, #subtitle-1, #subtitle-2, #second-image, #third-image").hide();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56459177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档