切换类.closest()
$(".toggle-more-less").click(function() { $(this).closest(".article").toggleClass("show-hide"); });
我正在尝试为智能手机大小的网站切换文章长度以节省空间。
我认为这会起作用 – 但我不明白.closest()我想。
这里是一个小伙子。
任何方向都是GRRRRREAT!
$(this)表示启动该function的项目,因此在这种情况下’$(“。toggle-more-less”)’。 最接近的将查找符合设定标准的最接近的$(this)父级,因此在这种情况下,它将向上遍历dom直到达到“数字”。
$(this).closest('figure').find("figcaption").toggleClass("show");
小提琴: http : //jsfiddle.net/6whzQ/
一个快速的解决方案是这样的: $(this).parent().find("figcaption").toggleClass("show");
$(this).prev().toggleClass("show"); $(this).prev("figcaption").toggleClass("show"); $(this).closest('figure').find("figcaption").toggleClass("show");
文档: