获取第二个<div>中对应的元素

huangapple go评论60阅读模式
英文:

Get corresponding element in second <div>

问题

(function ($) {
  $(&quot;.collapsible-title&quot;).on(&quot;click&quot;, function () {
    $(this).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-title&quot;).removeClass(&quot;is-active&quot;);
    $(&quot;.collapsible-content&quot;).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-content&quot;).removeClass(&quot;is-active&quot;);
  });
});
英文:

I have this format.

&lt;div class=&quot;container&quot; style=&quot;display: flex;&quot;&gt;
	&lt;div style=&quot;width: 25%;&quot;&gt;
		&lt;div class=&quot;collapsible-title is-active&quot;&gt;Title 1
			&lt;i class=&quot;fas fa-solid fa-chevron-right&quot;&gt;&lt;/i&gt;
		&lt;/div&gt;
		&lt;div class=&quot;collapsible-title&quot;&gt;Title 2
			&lt;i class=&quot;fas fa-solid fa-chevron-right&quot;&gt;&lt;/i&gt;
		&lt;/div&gt;
	&lt;/div&gt;
	&lt;div style=&quot;width: 75%;&quot;&gt;
		&lt;div class=&quot;collapsible-content is-active&quot;&gt;
			&lt;h3&gt;Title 1 Content&lt;/h3&gt;
			&lt;p&gt;
              Lorem Ipsum is simply dummy text.
            &lt;/p&gt;
		&lt;/div&gt;
		&lt;div class=&quot;collapsible-content&quot;&gt;
			&lt;h3&gt;Title 2 Content&lt;/h3&gt;
			&lt;p&gt;
              Lorem Ipsum has been the industry&#39;s standard dummy text.
            &lt;/p&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

Now when I click on "Title 2" on first div, I want to remove all "is-active" class, and put "is-active" class in "Title 2" form first div and div of "Title 2 content" in second div.

(function ($) {
  $(&quot;.collapsible-title&quot;).on(&quot;click&quot;, function () {
    $(this).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-title&quot;).removeClass(&quot;is-active&quot;);
    $(&quot;.collapsible-content&quot;).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-content&quot;).removeClass(&quot;is-active&quot;);
  });
});

答案1

得分: 1

If the order of the elements are the same then you can add .eq($(this).index()) to your code.

$(".collapsible-title").on("click", function() {
  $(this).addClass("is-active").siblings(".collapsible-title").removeClass("is-active");
  $(".collapsible-content").eq($(this).index()).addClass("is-active").siblings(".collapsible-content").removeClass("is-active");
});

This will add the class to the corresponding div to the clicked element.

英文:

If the order of the elements are the same then you can add .eq($(this).index()) to your code.

$(&quot;.collapsible-title&quot;).on(&quot;click&quot;, function() {
  $(this).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-title&quot;).removeClass(&quot;is-active&quot;);
  $(&quot;.collapsible-content&quot;).eq($(this).index()).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-content&quot;).removeClass(&quot;is-active&quot;);
});

This will add the class to the corresponding div to the clicked element.

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.collapsible-title&quot;).on(&quot;click&quot;, function() {
  $(this).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-title&quot;).removeClass(&quot;is-active&quot;);
  $(&quot;.collapsible-content&quot;).eq($(this).index()).addClass(&quot;is-active&quot;).siblings(&quot;.collapsible-content&quot;).removeClass(&quot;is-active&quot;);
});

<!-- language: lang-css -->

.collapsible-content {
  display: none;
}

.collapsible-content.is-active {
  display: block;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;container&quot; style=&quot;display: flex;&quot;&gt;
  &lt;div style=&quot;width: 25%;&quot;&gt;
    &lt;div class=&quot;collapsible-title is-active&quot;&gt;Title 1
      &lt;i class=&quot;fas fa-solid fa-chevron-right&quot;&gt;&lt;/i&gt;
    &lt;/div&gt;
    &lt;div class=&quot;collapsible-title&quot;&gt;Title 2
      &lt;i class=&quot;fas fa-solid fa-chevron-right&quot;&gt;&lt;/i&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;width: 75%;&quot;&gt;
    &lt;div class=&quot;collapsible-content is-active&quot;&gt;
      &lt;h3&gt;Title 1 Content&lt;/h3&gt;
      &lt;p&gt;
        Lorem Ipsum is simply dummy text.
      &lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;collapsible-content&quot;&gt;
      &lt;h3&gt;Title 2 Content&lt;/h3&gt;
      &lt;p&gt;
        Lorem Ipsum has been the industry&#39;s standard dummy text.
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译好的内容:

无论点击哪个 div,它的内容都会显示。您是否在寻找类似于这样的效果?

var title1Tab = document.getElementById('title1-tab');
var title2Tab = document.getElementById('title2-tab');
var title1Content = document.getElementById('title1-content');
var title2Content = document.getElementById('title2-content');

title1Tab.addEventListener('click', () => {
  title1Content.classList.remove('non-active');
  title1Content.classList.add('is-active');
  title2Content.classList.remove('is-active');
  title2Content.classList.add('non-active');
});

title2Tab.addEventListener('click', () => {
  title2Content.classList.remove('non-active');
  title2Content.classList.add('is-active');
  title1Content.classList.remove('is-active');
  title1Content.classList.add('non-active');
});
.is-active {
  display: block;
}

.non-active {
  display: none;
}
<div class="container" style="display: flex;">
  <div style="width: 25%;">
    <div class="collapsible-title is-active" id="title1-tab">Title 1
      <i class="fas fa-solid fa-chevron-right"></i>
    </div>
    <div class="collapsible-title" id="title2-tab">Title 2
      <i class="fas fa-solid fa-chevron-right"></i>
    </div>
  </div>
  <div style="width: 75%;">
    <div class="collapsible-content is-active" id="title1-content">
      <h3>Title 1 Content</h3>
      <p>
        Lorem Ipsum is simply dummy text.
      </p>
    </div>
    <div class="collapsible-content non-active" id="title2-content">
      <h3>Title 2 Content</h3>
      <p>
        Lorem Ipsum has been the industry's standard dummy text.
      </p>
    </div>
  </div>
</div>
英文:

Whichever div is clicked, its content is displayed. Are you looking for something like this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var title1Tab=document.getElementById(&#39;title1-tab&#39;);
var title2Tab=document.getElementById(&#39;title2-tab&#39;);
var title1Content=document.getElementById(&#39;title1-content&#39;);
var title2Content=document.getElementById(&#39;title2-content&#39;);

title1Tab.addEventListener(&#39;click&#39;,()=&gt;{
  title1Content.classList.remove(&#39;non-active&#39;);
  title1Content.classList.add(&#39;is-active&#39;);
  title2Content.classList.remove(&#39;is-active&#39;);
  title2Content.classList.add(&#39;non-active&#39;);
})
title2Tab.addEventListener(&#39;click&#39;,()=&gt;{
  title2Content.classList.remove(&#39;non-active&#39;);
  title2Content.classList.add(&#39;is-active&#39;);
  title1Content.classList.remove(&#39;is-active&#39;);
  title1Content.classList.add(&#39;non-active&#39;);
})

<!-- language: lang-css -->

.is-active{
  display:block;
}
.non-active{
  display:none;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot; style=&quot;display: flex;&quot;&gt;
    &lt;div style=&quot;width: 25%;&quot;&gt;
        &lt;div class=&quot;collapsible-title is-active&quot; id=&quot;title1-tab&quot;&gt;Title 1
            &lt;i class=&quot;fas fa-solid fa-chevron-right&quot;&gt;&lt;/i&gt;
        &lt;/div&gt;
        &lt;div class=&quot;collapsible-title&quot; id=&quot;title2-tab&quot;&gt;Title 2
            &lt;i class=&quot;fas fa-solid fa-chevron-right&quot;&gt;&lt;/i&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div style=&quot;width: 75%;&quot;&gt;
        &lt;div class=&quot;collapsible-content is-active&quot; id=&quot;title1-content&quot;&gt;
            &lt;h3&gt;Title 1 Content&lt;/h3&gt;
            &lt;p&gt;
              Lorem Ipsum is simply dummy text.
            &lt;/p&gt;
        &lt;/div&gt;
        &lt;div class=&quot;collapsible-content non-active&quot; id=&quot;title2-content&quot;&gt;
            &lt;h3&gt;Title 2 Content&lt;/h3&gt;
            &lt;p&gt;
              Lorem Ipsum has been the industry&#39;s standard dummy text.
            &lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 14:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定