jquery 没有父级的子选择器
我正在查看创建轮播菜单的教程中的一些代码,并注意到没有父项的父子选择器。以前从未见过这个,并且对它实际在做什么感到困惑。
请参阅以下代码:
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/
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.
See following code:
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);
Tutorial here: http://jqueryfordesigners.com/jquery-infinite-carousel/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个带有上下文的选择器:
翻转以使用
.find()
像这样:使用
>
child-selector 是只是:其他人也在做同样的事情,他们可以使用
.children()
,实际上这样做会更有效率。This selector with a context:
gets flipped around to use a
.find()
like this:which with the
>
child-selector is just:The others are doing the same deal, they could use
.children()
, and actually it'd be more efficient to do so.有一个父级(或者在本例中为
scope
),请注意选择器内的this
关键字,它与插件所应用的元素相关。jQuery 的选择器允许你设置一个范围,它可以是任何 jQuery 元素对象。
考虑
在插件内部
看一下我的一个问题,答案解释了很多关于 jQuery 选择器的内容。
在 jQuery 中选择后代元素最快的方法是什么?
There is a parent(or in this case a
scope
), notice thethis
keyword inside the selector, that's relative to the element the plugin is being applied to.jQuery's selectors allow you to set a scope, and it can be any jQuery element object.
Consider
And inside the plugin
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?
this
很重要。它是一个上下文参数,使查询等于参见
$()
<的文档/a> 了解更多信息;它特别提到了这一点:$(this).find('> div')
表示“作为this
子级的div
,等于 <代码>$(this).children('div')。The
this
is important. It's a context parameter that makes the query equal toSee the documentation for
$()
for more information; it specifically mentions this:$(this).find('> div')
means "thediv
s that are children ofthis
, which is equal to$(this).children('div')
.