【问题标题】:jQuery - using current object in a function calljQuery - 在函数调用中使用当前对象
【发布时间】:2015-07-10 05:17:37
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • 显示围绕该语句的代码。这是在事件处理程序中吗?
  • 没有围绕它的代码。它是正文末尾 <script> 标记中的唯一语句。

标签: javascript jquery css function this


【解决方案1】:

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

$(".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>

【讨论】:

  • 在打开这个问题之前,我已经达到了预期的结果:$(".split-section-headings .ssh-cell").html(function() { return "This container has padding-top = " + $(this).css("paddingTop") }); 但我认为它太脏了。我可以问你怎么看吗? :) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
相关资源
最近更新 更多