I'm trying to do this:
我正在尝试这样做:
$('.summary').css('top',$(this).outerHeight());
Where I reference 'this' I would like to reference the instance of $('.summary') that jquery is currently talking to, since there are multiple, but the scope considers 'this' the page in this instance.
我引用'this'的地方我想引用jquery当前正在讨论的$('。summary')实例,因为有多个,但是范围在这个实例中将'this'视为'this'。
What's the easiest way to reference each element in this case?
在这种情况下,引用每个元素最简单的方法是什么?
I looked through the search for the answer for this, but don't quite know the right phrasing. If this has been answered let me know and I can close as a duplicate.
我仔细查看了这个答案,但不太清楚正确的措辞。如果这已经回答,请告诉我,我可以作为副本关闭。
Thanks!
谢谢!
3 个解决方案
#1
6
You can pass a function directly to .css()
.
您可以将函数直接传递给.css()。
$('.summary').css('top',function() {
return $(this).outerHeight();
});
It will iterate the elements, and the return values will be the new values for 'top'
.
它将迭代元素,返回值将是'top'的新值。
#2
5
You're looking for .each()
, I think.
我想你正在寻找.each()。
$('.summary').each(function() {
var $this = $(this);
$this.css('top', $this.outerHeight());
});
EDIT: Less requerying. Also, I like am not i am
's answer more. It's slightly more elegant.
编辑:减少重新考验。此外,我喜欢我不是我的答案。它稍微优雅一点。
#3
1
You should define it as a variable first, then you can reference it in the method:
您应该首先将其定义为变量,然后您可以在方法中引用它:
var summary = $('.summary');
summary.css('top',summary.outerHeight());
#1
6
You can pass a function directly to .css()
.
您可以将函数直接传递给.css()。
$('.summary').css('top',function() {
return $(this).outerHeight();
});
It will iterate the elements, and the return values will be the new values for 'top'
.
它将迭代元素,返回值将是'top'的新值。
#2
5
You're looking for .each()
, I think.
我想你正在寻找.each()。
$('.summary').each(function() {
var $this = $(this);
$this.css('top', $this.outerHeight());
});
EDIT: Less requerying. Also, I like am not i am
's answer more. It's slightly more elegant.
编辑:减少重新考验。此外,我喜欢我不是我的答案。它稍微优雅一点。
#3
1
You should define it as a variable first, then you can reference it in the method:
您应该首先将其定义为变量,然后您可以在方法中引用它:
var summary = $('.summary');
summary.css('top',summary.outerHeight());