Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using the below code to click on an image within the .process class and when clicked the .process-info with toggle class of .process--shown.

This code works; however, it will toggle the class of process--shown for all elements on the page with the class of .process-info;

I only want the element clicked on to acitivate

$(document).ready(function(){

  $(".process .process--img").click(function(){
  $(".process .process--info").toggleClass("process--shown") 
});    
  $(".process .process--info").click(function(){
    $(".process .process--info").toggleClass("process--shown") 
  });    
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
305 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use this current element context to traverse up to common parent using .closest() then use .find()

$(".process .process--img").click(function () {
    $(this).closest(".process").find(".process--info").toggleClass("process--shown")
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...