jquery - 无法确定在 div 内表内的 td 上使用什么选择器

标签 jquery html css html-table

我无法确定使用哪个选择器来访问 CSS 属性。这是我的代码:

我的jquery:

<script type="text/javascript">
    $(document).ready(function () {
        $('#columnDay1').css('cursor', 'pointer');
        $('#columnDay1').mouseover(function () {
            $('td.calendarHeader').css("background-color", "#a43802");
        });
        $('#columnDay1').mouseout(function () {
            $('td.calendarHeader').css("background-color", "#37322e");
        });
    });
</script>

我的 html:

<div class="contentColumnDay1">
<table cellpadding="0" cellspacing="0" id="columnDay1">
    <tr>
        <td class="calendarHeader">
            <p><span class="dayHeader">Day 1</span><br />August 15, 2011</p>
        </td>
    </tr>
    <asp:Label runat="server" id="labelDay1"></asp:Label>
</table>

我正在尝试访问 .calendarHeader CSS 属性 background-color。我试过 #columnDay1.calendarHeader 但它不起作用。我用头撞 table 。

有什么帮助吗?

最佳答案

代替

$('#columnDay1').mouseover(function () {
    $('td.calendarHeader').css("background-color", "#a43802");
});

尝试

$('#columnDay1').mouseover(function () {
    $('td.calendarHeader', this).css("background-color", "#a43802");
});

td.calendarHeader 将引用类“calendarHeader”的每个 td。您需要为其提供 this$(this) 的上下文,以指定您只想在您刚刚鼠标悬停的元素内找到 td.calendarHeader。或者,您可以执行 $(this).find('td.calendarHeader').css

关于jquery - 无法确定在 div 内表内的 td 上使用什么选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6273565/

相关文章:

jquery - 从驻留在 <a> 标记内的图像中获取 "alt"属性

php - 优化 jQuery 速度

javascript - 浏览器调整大小后,将轮播保持在页面居中的网站上?

html - 带有超链接的 Angular 5 DomSanitizer

javascript - 为什么单击时未调用 javascript 代码

jquery - 响应式图片仅在一种情况下显示不佳

jquery - 将鼠标悬停在不透明菜单上

javascript - 使用 jquery 即时设置 js 下拉菜单的样式

html - 分区旋转不起作用

jquery - 异步加载 Google 地点