How can I reduce the amount of children I use in my jQuery functions?

后端 未结 5 2019
一生所求
一生所求 2021-01-31 20:10

I feel like I have to use way too many .children() in some of my jQuery functions.

Here\'s my HTML:

相关标签:
5条回答
  • 2021-01-31 20:27

    have you heard about .find() ?

    $('.goal-small-container').hover(function() {
      $(this).find('.goal-actions').css({visibility: "visible"});
    }, function () {
      $(this).find('.goal-actions').css({visibility: "hidden"});
    });
    
    0 讨论(0)
  • 2021-01-31 20:35

    Instead of

    $(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});
    

    You can use:

    $(this).find('> .goal-content > .goal-row > .goal-actions').css({visibility: "visible"});
    

    For exactly the same meaning. If there's no chance of that being ambiguous, however, (.goal-actions will only appear in that structure of the markup) you can just use find('.goal-actions').

    0 讨论(0)
  • 2021-01-31 20:40

    You can just use:

    $('.goal-small-container').hover(function() {
       $(this).find('goal-actions').show();
    }, function() {
       $(this).find('goal-actions').hide();
    });
    
    0 讨论(0)
  • 2021-01-31 20:41

    Why don't you just use .show() and .hide() on the second <div>? And, initially have them display hidden or something.

    0 讨论(0)
  • 2021-01-31 20:43
    .find('.goal-content .goal-row .goal-action').whatever()
    

    or more simply:

    .find('.goal-action').whatever()
    
    0 讨论(0)
提交回复
热议问题