使用jQuery单击特定子按钮。

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

Click On Specific Child Buttons Using jQuery

问题

我正在寻找当我点击每个图像时,导航到滑块元素内的特定滑块。活动幻灯片具有class="active",因此我只需要能够定位特定的按钮并添加class。

  1. <div>
  2. <img src="#" id="image_slide_1">
  3. <img src="#" id="image_slide_2">
  4. <img src="#" id="image_slide_3">
  5. <img src="#" id="image_slide_4">
  6. </div>
  7. <div class="pf-slider-nav">
  8. <button type="button" class="active"></button> //幻灯片1按钮
  9. <button type="button"></button> //幻灯片2按钮
  10. <button type="button"></button> //幻灯片3按钮
  11. <button type="button"></button> //幻灯片4按钮
  12. </div>

我尝试了类似以下的代码:

  1. $('#image_slide_4').on('click', function() {
  2. $('.pf-slider-nav button:nth-child(3)').addClass('active').click();
  3. });
英文:

I'm looking for when I click on each image, navigate to a specific slider inside the slider element. The active slide have the class="active" so I just need to be able to target the specific button and add the class.

  1. &lt;div&gt;
  2. &lt;img src=&quot;#&quot; id=&quot;image_slide_1&quot;&gt;
  3. &lt;img src=&quot;#&quot; id=&quot;image_slide_2&quot;&gt;
  4. &lt;img src=&quot;#&quot; id=&quot;image_slide_3&quot;&gt;
  5. &lt;img src=&quot;#&quot; id=&quot;image_slide_4&quot;&gt;
  6. &lt;/div&gt;
  7. &lt;div class=&quot;pf-slider-nav&quot;&gt;
  8. &lt;button type=&quot;button&quot; class=&quot;active&quot;&gt;&lt;/button&gt; //Slide 1 Button
  9. &lt;button type=&quot;button&quot;&gt;&lt;/button&gt; //Slide 2 Button
  10. &lt;button type=&quot;button&quot;&gt;&lt;/button&gt; //Slide 3 Button
  11. &lt;button type=&quot;button&quot;&gt;&lt;/button&gt; //Slide 4 Button
  12. &lt;/div&gt;

I was trying something like:

  1. $(&#39;#image_slide_4&#39;).on(&#39;click&#39;, function() {
  2. $(&#39;.pf-slider-nav button:nth-child(3)&#39;).addClass(&#39;active&#39;).click();
  3. });

答案1

得分: 1

  1. 在点击事件上使用通配符作为ID。

    $("[id^=image_slide]")
    idimage_slide 开头

  2. 单击图像时,捕获ID,分割并获取最后一个值。

    var ind = $(this).attr('id').split('_').pop();

  3. 删除按钮上以前添加的所有类,并借助步骤2找到按钮的索引并添加类。

    var _acBtn = $(".pf-slider-nav > button");
    _acBtn.removeClass("active"); // 删除以前的活动类
    _acBtn.eq(ind - 1).addClass("active"); // -1 因为索引从0开始

工作示例:

  1. $("[id^=image_slide]").on("click", function() {
  2. var ind = $(this).attr('id').split('_').pop();
  3. var _acBtn = $(".pf-slider-nav > button");
  4. _acBtn.removeClass("active"); // 删除以前的活动类
  5. _acBtn.eq(ind - 1).addClass("active"); // -1 因为索引从0开始
  6. });
  1. .active {
  2. background-color: red;
  3. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <div>
  3. <img src="#" id="image_slide_1">
  4. <img src="#" id="image_slide_2">
  5. <img src="#" id="image_slide_3">
  6. <img src="#" id="image_slide_4">
  7. </div>
  8. <div class="pf-slider-nav">
  9. <button type="button" class="active"></button> //Slide 1按钮
  10. <button type="button"></button> //Slide 2按钮
  11. <button type="button"></button> //Slide 3按钮
  12. <button type="button"></button> //Slide 4按钮
  13. </div>
英文:
  1. Use wild card for id on click event.

    1. $(&quot;[id^=image_slide]&quot;)

    id start with image_slide

  2. When click on image , catch id, split and and get last value.

    1. var ind = $(this).attr(&#39;id&#39;).split(&#39;_&#39;).pop();
  3. Delete all previous added class on button and help of step 2 find index of button and add class .

    1. var _acBtn = $(&quot;.pf-slider-nav &gt; button&quot;);
    2. _acBtn.removeClass(&quot;active&quot;); //Remove previous active class
    3. _acBtn.eq(ind - 1).addClass(&quot;active&quot;); // -1 because index start with 0

Working example:

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

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

  1. $(&quot;[id^=image_slide]&quot;).on(&quot;click&quot;, function() {
  2. var ind = $(this).attr(&#39;id&#39;).split(&#39;_&#39;).pop();
  3. var _acBtn = $(&quot;.pf-slider-nav &gt; button&quot;);
  4. _acBtn.removeClass(&quot;active&quot;); //Remove previous active class
  5. _acBtn.eq(ind - 1).addClass(&quot;active&quot;); // -1 because index start with 0
  6. });

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

  1. .active {
  2. background-color: red;
  3. }

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

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;div&gt;
  3. &lt;img src=&quot;#&quot; id=&quot;image_slide_1&quot;&gt;
  4. &lt;img src=&quot;#&quot; id=&quot;image_slide_2&quot;&gt;
  5. &lt;img src=&quot;#&quot; id=&quot;image_slide_3&quot;&gt;
  6. &lt;img src=&quot;#&quot; id=&quot;image_slide_4&quot;&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;pf-slider-nav&quot;&gt;
  9. &lt;button type=&quot;button&quot; class=&quot;active&quot;&gt;&lt;/button&gt; //Slide 1 Button
  10. &lt;button type=&quot;button&quot;&gt;&lt;/button&gt; //Slide 2 Button
  11. &lt;button type=&quot;button&quot;&gt;&lt;/button&gt; //Slide 3 Button
  12. &lt;button type=&quot;button&quot;&gt;&lt;/button&gt; //Slide 4 Button
  13. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

这是Vanilla JS中的另一种解决方案:

  1. <div>
  2. <img src="#" id="image_slide_1">
  3. <img src="#" id="image_slide_2">
  4. <img src="#" id="image_slide_3">
  5. <img src="#" id="image_slide_4">
  6. </div>
  7. <div class="pf-slider-nav">
  8. <button type="button" class="active">1</button> <!-- Slide 1 Button -->
  9. <button type="button">2</button> <!-- Slide 2 Button -->
  10. <button type="button">3</button> <!-- Slide 3 Button -->
  11. <button type="button">4</button> <!-- Slide 4 Button -->
  12. </div>
  1. const btns = document.querySelectorAll("button");
  2. document.querySelector("div")
  3. .querySelectorAll("img").forEach((el, i) => el.onclick = () => btns.forEach((b, j) => b.classList.toggle("active", j == i)));
  1. .active {
  2. background-color: red;
  3. }

请注意,这只是HTML、JavaScript和CSS代码的翻译部分。

英文:

Here is another solution in Vanilla JS:

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

<!-- language: lang-js -->
const btns=document.querySelectorAll("button");
document.querySelector("div")
.querySelectorAll("img").forEach((el,i)=>el.onclick=()=>btns.forEach((b,j)=>b.classList.toggle("active",j==i)))

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

  1. .active {
  2. background-color: red;
  3. }

<!-- language: lang-html -->
<div>
<img src="#" id="image_slide_1">
<img src="#" id="image_slide_2">
<img src="#" id="image_slide_3">
<img src="#" id="image_slide_4">
</div>

  1. &lt;div class=&quot;pf-slider-nav&quot;&gt;
  2. &lt;button type=&quot;button&quot; class=&quot;active&quot;&gt;1&lt;/button&gt; //Slide 1 Button
  3. &lt;button type=&quot;button&quot;&gt;2&lt;/button&gt; //Slide 2 Button
  4. &lt;button type=&quot;button&quot;&gt;3&lt;/button&gt; //Slide 3 Button
  5. &lt;button type=&quot;button&quot;&gt;4&lt;/button&gt; //Slide 4 Button
  6. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月14日 08:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76684063.html
匿名

发表评论

匿名网友

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

确定