jQuery - 在函数调用中使用当前对象

时间:2015-07-10 05:17:37

标签: javascript jquery css function this

我想让这段代码工作,但.css函数正在返回undefined

$(".split-section-headings .ssh-cell")
    .html("This container has padding-top = " + $(this).css("paddingTop") );

谢谢!

1 个答案:

答案 0 :(得分:2)

如果没有类似于$(this)的内容或在事件处理程序中,则无法使用.each()

$(".split-section-headings .ssh-cell").each(function() {
  $(this).html("This container has padding-top = " + $(this).css("padding"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="split-section-headings">
  <div class="ssh-cell"></div>
</div>