Jquery next adjacent selector with $(this)

2020-07-06 07:52发布

How could i use the adjacent selector "+" with the $(this).

I would need a help with the commented lines with //this doesnt work:

$(".ExpandCollapse").click(function () {
            if ($(this).nextUntil('.Collapsable').is(':visible'))
            {
                //this doesnt work 
                $(this + ".Collapsable").hide();
            }
            else
            {
                //this doesnt work
                $(this + ".Collapsable").show();
            }
        });

Could you give me a hand?

Thanks a lot in advance.

Best Regards.

Jose

3条回答
Rolldiameter
2楼-- · 2020-07-06 08:18

this is a reference to the DOM element of invocation. You can't concat a string to that.

So you either can directly use this to act on it

$(this).hide();

or you can walk through the DOM from there

$(this).next().hide();
$(this).prev().hide();
$(this).closest('.Collapsable').hide();
// another 200 methods
查看更多
啃猪蹄的小仙女
3楼-- · 2020-07-06 08:25

Use next()

$(this).next(".Collapsable").hide();

Or simply:

$(this).next().hide();
查看更多
神经病院院长
4楼-- · 2020-07-06 08:27

You can also cut down on having two statements for hiding and showing:

$(this).next().toggle();
查看更多
登录 后发表回答