.closest()的切换类 [英] toggle class of .closest()

查看:90
本文介绍了.closest()的切换类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    $(".toggle-more-less").click(function() {
        $(this).closest(".article").toggleClass("show-hide");          
    });

我正在尝试为智能手机大小的网站切换文章长度,以节省空间.

I am trying to toggle article length for a smartphone size site to save space.

我以为这可以用-但我不明白.closest()我想.

I thought that this would work - but I don't understand .closest() I guess.

这里是一个jsfiddle.

HERE is a jsfiddle.

任何方向都是可怕的!

推荐答案

$(this)表示启动函数的项,因此在本例中为'$(.toggle-more-less")'. 最接近的对象将寻找最符合设置条件的$(this)的父对象,因此在这种情况下,它将向上遍历dom,直到达到数字".

$(this) means the items that initiated the function, so in this case '$(".toggle-more-less")'. Closest will look for the closest parent to $(this) that fits the set criteria, so in this case it will traverses the dom upwards until it reaches a 'figure'.

$(this).closest('figure').find("figcaption").toggleClass("show");

提琴: http://jsfiddle.net/6whzQ/

这篇关于.closest()的切换类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆