最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Using $(this).find with .click() jQuery - Stack Overflow

matteradmin4PV0评论

On the click of the plus icon I want the check icon to appear for only the one plus icon in that particular div. Not all plus icons throughout the page. Here is the html followed by the jQuery.

<div class="pin">
        <img class="brand-logo" src="img/nike.jpg" />
        <div class= "hover-popup">
            <h1 class="brand-name">Nike</h1>
            <img class="plus-icon" src="img/plus-icon.png" />
            <img class="check-icon" src="img/check-icon.png" />
        </div>
    </div>

    <div class="pin">
        <img class="brand-logo" src="img/calvinklein.jpg" />
        <div class= "hover-popup">
            <h1 class="brand-name">Calvin Klein</h1>
            <img class="plus-icon" src="img/plus-icon.png" />
            <img class="check-icon" src="img/check-icon.png" />
        </div>

jQuery

$(".plus-icon").click(
    function () { $(this).find('.check-icon').show(); },
    function () { $(this).find('.plus-icon').hide(); }
);

$(".check-icon").click(function() {
    function () { $(this).find('.plus-icon').show(); },
    function () { $(this).find('.check-icon').hide(); }
);

On the click of the plus icon I want the check icon to appear for only the one plus icon in that particular div. Not all plus icons throughout the page. Here is the html followed by the jQuery.

<div class="pin">
        <img class="brand-logo" src="img/nike.jpg" />
        <div class= "hover-popup">
            <h1 class="brand-name">Nike</h1>
            <img class="plus-icon" src="img/plus-icon.png" />
            <img class="check-icon" src="img/check-icon.png" />
        </div>
    </div>

    <div class="pin">
        <img class="brand-logo" src="img/calvinklein.jpg" />
        <div class= "hover-popup">
            <h1 class="brand-name">Calvin Klein</h1>
            <img class="plus-icon" src="img/plus-icon.png" />
            <img class="check-icon" src="img/check-icon.png" />
        </div>

jQuery

$(".plus-icon").click(
    function () { $(this).find('.check-icon').show(); },
    function () { $(this).find('.plus-icon').hide(); }
);

$(".check-icon").click(function() {
    function () { $(this).find('.plus-icon').show(); },
    function () { $(this).find('.check-icon').hide(); }
);
Share Improve this question edited Dec 14, 2015 at 20:12 Alexander O'Mara 60.7k19 gold badges173 silver badges181 bronze badges asked Dec 27, 2014 at 0:43 Calvin HemingtonCalvin Hemington 431 gold badge1 silver badge8 bronze badges 4
  • Use a direct selection instead of finding the element – user4396006 Commented Dec 27, 2014 at 0:47
  • In the element clicked just don't find it, use this directly and in the other one, go to the parent and then get the children and then find the element – user4396006 Commented Dec 27, 2014 at 0:48
  • BTW I think find should return an array. Am I right? – user4396006 Commented Dec 27, 2014 at 0:48
  • .find() is for searching for nested elements. There's nothing nested inside the icon. – Barmar Commented Aug 11, 2024 at 17:07
Add a ment  | 

5 Answers 5

Reset to default 0

First find the containing div, since its an ancestor of .check-icon,.plus-icon use .closest(), then you can use find()

$(this).closest('.hover-popup').find('.check-icon').show();

You could use jQuery's siblings selector.

$(".plus-icon").click(
    function () { $(this).siblings('.check-icon').show(); },
    function () { $(this).hide(); }
);

$(".check-icon").click(function() {
    function () { $(this).siblings('.plus-icon').show(); },
    function () { $(this).hide(); }
);

Given your example HTML, this would be the preferred solution.

Alternately, use .parent, then use find to descend from there.

$(".plus-icon").click(
    function () { $(this).parent().find('.check-icon').show(); },
    function () { $(this).parent().find('.plus-icon').hide(); }
);

$(".check-icon").click(function() {
    function () { $(this).parent().find('.plus-icon').show(); },
    function () { $(this).parent().find('.check-icon').hide(); }
);

Finally, if you are not sure where the .hover-popup element will be relative to the button, use .closest to locate it, then descend from there.

$(".plus-icon").click(
    function () { $(this).closest('.hover-popup').find('.check-icon').show(); },
    function () { $(this).closest('.hover-popup').find('.plus-icon').hide(); }
);

$(".check-icon").click(function() {
    function () { $(this).closest('.hover-popup').find('.plus-icon').show(); },
    function () { $(this).closest('.hover-popup').find('.check-icon').hide(); }
);

Using Jquery your code will look like:

$(document).ready(function(){     
    $('.plus-icon').click(function(){
        $(this).hide();
        $(this).siblings('.check-icon').show()
    })
    $('.check-icon').click(function(){
        $(this).hide();
        $(this).siblings('.plus-icon').show()
    })
})

try this

$(".plus-icon").click(
    function () { $(this).next('.check-icon').show(); },
    function () { $(this).hide(); }
);

$(".check-icon").click(function() {
    function () { $(this).prev('.plus-icon').show(); },
    function () { $(this).hide(); }
);

Probably the easiest way is to use .siblings() function.

e.g

$('.plus-icon').click(function(){
   $(this).siblings('.check-icon').show();
});
Post a comment

comment list (0)

  1. No comments so far