简体   繁体   English

如何覆盖css属性?

[英]How to override css property?

I have tried to use jquery, but could not override the width property of the .gridHeader class. 我试图使用jquery,但无法覆盖.gridHeader类的width属性。

I'd like to fix (let's say 100px) the width of the first column in the table. 我想修复(比如说100px)表格中第一列的宽度。

<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>

You need to remove the inline style with removeAttr and then use the css() method like this: 您需要使用removeAttr删除内联样式,然后使用css()方法,如下所示:

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

If you want to apply the width to first column only, you can use the :first filter selector like this: 如果要仅将宽度应用于第一列,可以使用:first filter选择器,如下所示:

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

I guess your problem is not the first column, but the others, while they still have 100% applied. 我猜你的问题不是第一列,而是其他人,虽然他们仍然100%应用。

$(".gridHeader").css("width","");

...won't work, because you did not set a legal value. ......不会起作用,因为你没有设定合法的价值。

Try this: 尝试这个:

$(".gridHeader").not(':first-child').css("width","auto");

Works fine for me? 适合我吗? Unless I don't understand your "problem". 除非我不明白你的“问题”。

Check: http://www.jsfiddle.net/YjC6y/21/ 检查: http//www.jsfiddle.net/YjC6y/21/

如果你想覆盖CSS内联,你可能会这样运气:

style="width:100px !important;"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM