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

标签 javascript jquery css function this

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

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

谢谢!

最佳答案

如果没有类似于 .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>

关于javascript - jQuery - 在函数调用中使用当前对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31332974/

相关文章:

javascript - 进行全局正则表达式替换时消除不稳定

javascript - 使用 Skype SDK 聊天时消息出现两次

php - 无法使用ajax将变量发送到php

javascript - 如何在不移动列表中其他元素的情况下使文本出现在 div 下?

javascript - 使用css将标题分成jquery中的列

javascript - div 元素应该向下滚动直到它与另一个元素接触

javascript - 我如何要求用户确认他们要离开页面?

javascript - 如何将多条线添加到 D3JS 折线图中,其中因变量数据源从范围内的不同位置开始?

jquery - 使用 jquery jplayer 插件播放 mp3

javascript - 通过 Javascript 添加的 CSS 链接填充在 IE 中不起作用