使用jQuery单击特定子按钮。

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

Click On Specific Child Buttons Using jQuery

问题

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

<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>

<div class="pf-slider-nav">
   <button type="button" class="active"></button> //幻灯片1按钮
   <button type="button"></button>                //幻灯片2按钮
   <button type="button"></button>                //幻灯片3按钮
   <button type="button"></button>                //幻灯片4按钮
</div>

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

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

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.

&lt;div&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_1&quot;&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_2&quot;&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_3&quot;&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_4&quot;&gt;
&lt;/div&gt;

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

I was trying something like:

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

答案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开始

工作示例:

$("[id^=image_slide]").on("click", function() {
  var ind = $(this).attr('id').split('_').pop();
  var _acBtn = $(".pf-slider-nav > button");
  _acBtn.removeClass("active"); // 删除以前的活动类
  _acBtn.eq(ind - 1).addClass("active"); // -1 因为索引从0开始
});
.active {
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>

<div class="pf-slider-nav">
  <button type="button" class="active"></button> //Slide 1按钮
  <button type="button"></button> //Slide 2按钮
  <button type="button"></button> //Slide 3按钮
  <button type="button"></button> //Slide 4按钮
</div>
英文:
  1. Use wild card for id on click event.

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

    id start with image_slide

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

    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 .

    var _acBtn = $(&quot;.pf-slider-nav &gt; button&quot;);
    _acBtn.removeClass(&quot;active&quot;); //Remove previous active class
    _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 -->

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

});

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

.active {
  background-color: red;
}

<!-- 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&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_1&quot;&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_2&quot;&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_3&quot;&gt;
  &lt;img src=&quot;#&quot; id=&quot;image_slide_4&quot;&gt;
&lt;/div&gt;

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

<!-- end snippet -->

答案2

得分: 0

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

<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>

<div class="pf-slider-nav">
  <button type="button" class="active">1</button> <!-- Slide 1 Button -->
  <button type="button">2</button> <!-- Slide 2 Button -->
  <button type="button">3</button> <!-- Slide 3 Button -->
  <button type="button">4</button> <!-- Slide 4 Button -->
</div>
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)));
.active {
  background-color: red;
}

请注意,这只是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 -->

.active {
  background-color: red;
}

<!-- 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>

&lt;div class=&quot;pf-slider-nav&quot;&gt;
  &lt;button type=&quot;button&quot; class=&quot;active&quot;&gt;1&lt;/button&gt; //Slide 1 Button
  &lt;button type=&quot;button&quot;&gt;2&lt;/button&gt; //Slide 2 Button
  &lt;button type=&quot;button&quot;&gt;3&lt;/button&gt; //Slide 3 Button
  &lt;button type=&quot;button&quot;&gt;4&lt;/button&gt; //Slide 4 Button
&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:

确定