【发布时间】:2026-02-21 20:45:02
【问题描述】:
只有当它是另一个元素中的第一个元素时,如何选择一个元素?在下面的示例中,如果 h3 只是第一个元素,我想选择并应用 0 的 margin-top。
我认为这会起作用:
$('.flexible-content-container .each-flexible-section .text-container h3').each(function() {
if ( $(this).is(':first-of-type') ) {
$(this).css('margin-top','0');
}
});
但这会选择 div 中的第一个 h3。于是我尝试了:
$('.flexible-content-container .each-flexible-section .text-container h3').each(function() {
if ( $(this).is(':first') ) {
$(this).css('margin-top','0');
}
});
但这似乎没有选择任何东西。
有什么想法吗?
【问题讨论】:
-
$('.flexible-content-container .each-flexible-section .text-container h3:first').css('margin-top','0');- 使用这个 -
@rdck 这取决于你的 html 标记,你介意也发布一下吗。
标签: jquery html jquery-selectors