Targeting multiple elements with jQuery

后端 未结 5 2045
无人及你
无人及你 2021-02-06 04:34

I have been searching but have come up blank and i\'m wondering if I can use one jQuery statement to target multiple elements on a page. I have several identical buttons on a pa

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 05:14

    You can do:

    $(".learnMoreButton, .bioButton").hover(function() {
      $(this).find(".buttonRight")...
      ...
    }, function() {
      ...
    });
    

    I will add that I think you'd be better off doing that with CSS classes.

    .buttonLeft { background: url(/images/concaveBtn-Left.gif) }
    .buttonMiddle { background-image: url(/images/concaveBtn-Middle.gif) }
    .buttonMiddle a { color: #666; }
    .buttonRight { url(/images/concaveBtn-Right.gif) }
    .hoverover .buttonLeft { url(/images/concaveBtn-Left2.gif) }
    .hoverover .buttonMiddle { url(/images/concaveBtn-Middle2.gif) }
    .hoverover .buttonMiddle a { color: #FFF; }
    .hoverover .buttonRight { background: url(/images/concaveBtn-Right2.gif) }
    

    and

    $(".learnMoreButton, .bioButton").hover(function() {
      $(this).addClass("hoverover");
    }, function() {
      $(this).removeClass("hoverover");
    });
    

    and you'll have a lot less code.

    Also you can give elements multiple classes so:

    ...
    ...

    and then it becomes even simpler:

    $(".hoverbutton").hover(function() {
      $(this).addClass("hoverover");
    }, function() {
      $(this).removeClass("hoverover");
    });
    

提交回复
热议问题