jQuery:获取给定“y”上方的元素位置
如何使用 jQuery 以优雅的方式做到这一点?
将
z
属性(例如:红色背景)应用于 div父级
的每个子级
而它们的位置高于给定的顶部偏移y
。
我尝试过不同的方式,但我对其中任何一个都不满意......
我知道必须有一种简短而优雅的方法来做到这一点......
How can you do this with jQuery, in an elegant way?
Apply
z
attribute (e.g.: red background) to every children of a divparent
while their position is above a given top-offsety
.
I've tried in different ways, but I'm not happy with any of them...
I know there must be a short and elegant way to do it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然你说你已经尝试了几种方法,并且你只是在寻找更优雅的方法,那么我假设你已经解决了偏移部分,并且我将使用
offset
我自己。根据需要修改该部分。为了优雅,您可以创建一个自定义选择器检查顶部偏移量:然后您可以这样查询它:
当然,这也可以表达为
or ,正如注释中指出的那样
Since you're saying you've tried a few ways, and you're just looking for something more elegant, I'll assume you have the offset part worked out, and I'll just go with
offset
myself. Modify that part as needed. For elegance, you could create a custom selector checking top offset:You could then query it as such:
Of course this could just as well have been expressed as
or, as pointed out in comments
像这样的事情应该可以完成工作:
选择器
'#parent > *'
将选择 idparent
的元素的所有直接子元素(不是所有后代)。我认为这就是您正在寻找的,因为您说过“适用于 divparent
的每个子级。”演示:http://jsfiddle.net/mattball/87QFU/1/
Something like this should get the job done:
The selector
'#parent > *'
will select all immediate children (not all descendants) of the element with idparent
. I assume that's what you're looking for, since you said "apply... to every children of a divparent
."Demo: http://jsfiddle.net/mattball/87QFU/1/
孩子们是动态放置的顶部偏移还是他们都有一个共同的CSS类?
Are the childrens dynamically placed with top-offset or do they all have a common css-class?