jquery - 如何覆盖CSS属性?

标签 jquery html css properties

我曾尝试使用 jquery,但无法覆盖 .gridHeader 类的 width 属性。

我想固定(假设为 100 像素)表格中第一列的宽度。

<html>
    <head>
        <style>
            html, body {
                font-family:Tahoma;
                font-size:11px;
            }
            .grid {
                border-left:1px solid #CCCCCC;
                border-top:1px solid #CCCCCC;
            }
            .gridHeader {
                background-color:#EFEFEF;
                border-bottom:1px solid #CCCCCC;
                font-weight:bold;
                height:20px;
                padding-left:3px;
                text-align:left;
                width:100%; /* problematic, that I cannot change*/
            }
            table.grid > tbody > tr > td {
                border-bottom:1px solid #CCCCCC;
                border-right:1px solid #CCCCCC;
                padding:3px;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $(".gridHeader").css("width","");
            });
        </script>
    </head>
<body>
    <table width="100%">
        <tr>
            <td class="gridHeader" style="width:100px">Vikas
            </td>
            <td class="gridHeader">Vikas Patel Vikas Patel Vikas Patel
            </td>
            <td class="gridHeader">Vikas Patel Vikas Patel
            </td>
        </tr>
        <tr>
            <td>Vikas
            </td>
            <td>Vikas Patel Vikas Patel Vikas Patel
            </td>
            <td>Vikas Patel Vikas Patel
            </td>
        </tr>
    </table>
</body>
</html>

最佳答案

您需要使用 removeAttr 删除内联样式然后像这样使用 css() 方法:

$(".gridHeader").removeAttr('style').css("width","100px");

如果您只想将宽度应用到第一列,您可以使用 :first像这样的过滤器选择器:

$(".gridHeader:first").removeAttr('style').css("width","100px");

关于jquery - 如何覆盖CSS属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3930304/

相关文章:

html - 当 ng-view 内容大于窗口高度时出现边框问题

javascript - 无限滚动在 IE 中返回多个结果

html - 使用 CSS 增加水平菜单项之间的间距时遇到问题

javascript - 使用 jQuery Slice 从下拉菜单中显示前 N 个项目

html - 移动布局中的 Div 顺序 - Bootstrap 4 行和列

html - 我如何使用 Bootstrap 在输入框中添加按钮

html - 背景附件 : fixed not working

javascript - Jquery 列生成器插件 : 40 times faster in Windows Firefox. 为什么?

Javascript 原型(prototype) - 需要帮助添加 Helper 原型(prototype)

javascript - getJSON 完成回调