javascript - jQuery 用选定的一个分隔 div

标签 javascript jquery html css

我有多个具有相同 div id 的 div,但我想在激活任何主 div 时将每个 div 分开,它将显示其子 div,我尝试了很多功能,但对我没有任何作用。这是我的代码

CSS

.active{
    border-style:solid;
    border-width:1px;
    border-color:black;
}

JS

    $('div[id^="show"]').hide();
    $("div").click(function(){
    $('div').removeClass('active');
    $(this).addClass('active');
        setTimeout(function(){
                $('div[id^="show"]').show();
            setTimeout(function() {
        $('div[id^="show"]').hide()
    }, 5000);
        }, 3000);
    });
});

HTML

<div class="div">
content 1
<div id="show">ad here1</div>
</div>
<div class="div">
content 2
<div id="show">ad here2</div>
</div>
<div class="div">
content 3
<div id="show">ad here3</div>
</div>
<div class="div">
content 4
<div id="show">ad here4</div>
</div>

我真正想要的是,当有人点击具有文本“content 1”的父 div 时,其具有 id“show”的子 div 将出现并关闭,而不是所有具有 id“show”的 div

最佳答案

看到输出希望它会有所帮助,我已经省略了超时,你可以再次插入它们

  • 需要在点击的div中找到id显示的div
  • 然后隐藏其他id为show的div,并使用not()过滤掉自己id为show的子元素
  • 在#show 中使用 class 而不是 id,更正它

$('div#show').hide();
$("div").click(function() {
  $('div').removeClass('active');
  $(this).addClass('active');
  
    $neardiv = $(this).find('div#show');
    $neardiv.show();
   
      $('div#show').not($neardiv).hide()
    

});
.active {
  border-style: solid;
  border-width: 1px;
  border-color: black;
  color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



<div class="div">
  content 1
  <div id="show">ad here1</div>
</div>
<div class="div">
  content 2
  <div id="show">ad here2</div>
</div>
<div class="div">
  content 3
  <div id="show">ad here3</div>
</div>
<div class="div">
  content 4
  <div id="show">ad here4</div>
</div>

关于javascript - jQuery 用选定的一个分隔 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27453132/

相关文章:

javascript - 如何使lightslider jquery插件在reactjs组件中工作?

jquery - 使用 "columns.data"和 JSON 数据右对齐 JQuery 数据表单元格?

html - CSS 动画在 HTA 中不起作用

html - 如何将背景图像添加到 Font Awesome 图标?

javascript print() 方法未按预期工作

javascript - 如何在 typescript 中定义 5 个元素的有限集?

javascript - 按住鼠标连续增加值

Javascript + 区分用户点击和从函数发送的点击

javascript - 多个模态窗口尝试在 JS 中使用正则表达式

javascript - 具有多个下拉菜单的子菜单关闭延迟