没有父级的jQuery子级选择器 [英] jquery child selector without parent
问题描述
我正在看教程中的一些代码,用于创建轮播菜单,并注意到没有父级的父级子级选择器.以前从未见过,并对其实际操作感到困惑.
I was looking at some code from a tutorial for creating a carousel menu and noticed parent child selectors without the parent. Never seen this before, and confused to what it is actually doing.
请参见以下代码:
var $wrapper = $('> div', this).css('overflow', 'hidden'),
$slider = $wrapper.find('> ul'),
$items = $slider.find('> li'),
$single = $items.filter(':first'),
singleWidth = $single.outerWidth(),
visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
currentPage = 1,
pages = Math.ceil($items.length / visible);
此处的教程: http://jqueryfordesigners.com/jquery-infinite-carousel/
推荐答案
有一个父项(在本例中为scope
),请注意选择器中的this
关键字,该关键字与插件所在的元素有关应用于.
There is a parent(or in this case a scope
), notice the this
keyword inside the selector, that's relative to the element the plugin is being applied to.
jQuery的选择器允许您设置范围,它可以是任何jQuery元素对象.
jQuery's selectors allow you to set a scope, and it can be any jQuery element object.
考虑
$(".somediv").myplugin();
在插件内部
$("> div", this)
is actually translated to
$("> div", $(".somediv"))
看看我的一个问题,答案在很大程度上解释了jQuery的选择器. 在jQuery中选择后代元素最快的方法是什么?
Have a look at one of my questions, the answer explains quite a bit about jQuery's selectors. What is the fastest method for selecting descendant elements in jQuery?
这篇关于没有父级的jQuery子级选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!