简体   繁体   English

如何更改HTML表的td宽度?

[英]How to change HTML table td width?

Here's my current code, 这是我当前的代码,

$(document).ready(function () {
    $(".stripeMe tr").mouseover(function () {
        $(this).addClass("over");
    }).mouseout(function () {
        $(this).removeClass("over");
    });
    $(".stripeMe tr:even").addClass("alt");
});


<table class="stripeMe" width="100%">
    <tr>
        <td style="width:85%">Product Name</td>
        <td style="width:5%; text-align:right">Price (each)</td>
        <td style="width:5%; text-align:center">Quantity</td>
        <td style="width:5%"></td>
    </tr>

    <% foreach (var cart in Model.Carts) { %>
        <tr id="row-<%: cart.RecordID %>">
            <td>
                <%: Html.ActionLink(Model.pr.GetProduct(cart.ProductID).ProductName.Length > 52 ? (string)Model.pr.GetProduct(cart.ProductID).ProductName.Substring(0, 52) + "..." :  (string)Model.pr.GetProduct(cart.ProductID).ProductName, "Elaborate", "Product", new { ProductID = cart.ProductID, RecordID = cart.RecordID }, null)%>
            </td>
            <td style="text-align:right">
                <%: (string)String.Format("{0:F}",Model.pr.GetProduct(cart.ProductID).UnitPrice) %>
            </td>
            <td>
                <input type="text" disabled="disabled" style="text-align:center; border-width:0; background-color:transparent" id="column-Quantity-<%: cart.RecordID %>" value="<%: cart.Quantity %>"></input>
            </td>
            <td>
                <%: Ajax.ActionLink("Remove from Cart", "RemoveFromCart", new { RecordID = cart.RecordID }, new AjaxOptions {OnSuccess = "handleUpdate" })%>
            </td>
        </tr>
    <% } %>

    <tr>
        <td><hr />Total</td>
        <td align="right">
            <hr />
            <span id="cart-total"><%: (string)String.Format("{0:F}",Model.CartTotal) %></span>
         </td>
         <td align="center">
             <hr />
             <span id="cart-quantity"><%: Model.CartQuantity %></span>
         </td>
         <td><hr />&nbsp;</td>
     </tr>
 </table>

No matter how I change it, it doesn't work. 无论我如何更改,它都不起作用。 I'm using the latest version of Firefox. 我正在使用最新版本的Firefox。

<td style="width:85%">Product Name</td>

Try adding table-layout:fixed style to the table, as such, 尝试将table-layout:fixed样式添加到表中,这样,

<table class="stripeMe" width="100%" style="table-layout:fixed">

to make the change more apparent. 使变化更加明显。

However, using this setting will overflow the content into adjacent td . 但是,使用此设置会将内容溢出到相邻的td

Here is a working fiddle: http://jsfiddle.net/DEU7d/1/ 这是一个有效的小提琴: http : //jsfiddle.net/DEU7d/1/

The class .alt is getting added to 'even' rows on page load. 类.alt将在页面加载时添加到“偶数”行。 (i added a color for .alt) (我为.alt添加了颜色)

The class .over is getting toggled between mouse over and out . .over类将在mouse over和out之间切换。

CSS 的CSS

.over { background:#88aa4d; }
.alt { color: #ff881a; }

JS JS

$(document).ready(function () {

$(".stripeMe tr").mouseover(function () {
    $(this).addClass("over");
}).mouseout(function () {
    $(this).removeClass("over");
});

$(".stripeMe tr:even").addClass("alt");
});

HTML 的HTML

<table class="stripeMe" width="100%">
    <tr>
        <td style="width:85%">Product Name</td>
        <td style="width:5%; text-align:right">Price (each)</td>
        <td style="width:5%; text-align:center">Quantity</td>
        <td style="width:5%"></td>
    </tr>
        <tr id="row-1">
            <td>
                linktext1
            </td>
            <td style="text-align:right">
               price
            </td>
            <td>
                <input type="text" disabled="disabled" style="text-align:center; border-width:0; background-color:transparent" id="column-Quantity-1" value="12"></input>
            </td>
            <td>
                iink text2
            </td>
        </tr>
      <tr class="even" id="row-2">
            <td>
                linktext1
            </td>
            <td style="text-align:right">
               price
            </td>
            <td>
                <input type="text" disabled="disabled" style="text-align:center; border-width:0; background-color:transparent" id="column-Quantity-2" value="21"></input>
            </td>
            <td>
                iink text2
            </td>
        </tr>


    <tr>
        <td><hr />Total</td>
        <td align="right">
            <hr />
            <span id="cart-total">total</span>
         </td>
         <td align="center">
             <hr />
             <span id="cart-quantity">quantity</span>
         </td>
         <td><hr />&nbsp;</td>
     </tr>
 </table>

First I would like say "THANK YOU" to everybody who put effort to help me out, especially thanks to "rae1n", "Deminoth Bono", "tristan" and "OMG" !!! 首先,我想对所有努力帮助我的人说“谢谢”,尤其是感谢“ rae1n”,“ Deminoth Bono”,“ tristan”和“ OMG”! all you guys effort help me figure out the issue and solve it. 你们所有的努力都可以帮助我找出问题并解决。

First I use firefox "error console", I find a javascript issue which is sit there long time, then I fix it, but unfortunately our "Width" issue still there. 首先,我使用firefox“错误控制台”,找到了一个放置了很长时间的javascript问题,然后修复了它,但不幸的是,我们的“ Width”问题仍然存在。

Second from "rae1n" and "OMG" post, I start to thought this may not an " width" issue, so I start to look around, after one day thinking......, finally I figure out the issue is at input type="text" tag. 其次,从“ rae1n”和“ OMG”帖子中,我开始认为这可能不是“宽度”问题,因此,经过一天的思考,我开始环顾四周……,最后我发现问题出在输入type =“ text”标签。 You know, if you do not give a width in the input type="text", it will render an textbox with fix width looks like 200px, this cause the issue. 您知道,如果在输入type =“ text”中不指定宽度,它将呈现固定宽度为200px的文本框,这会引起问题。 So what ever how I change the "td" percentage, it still keep same looking. 因此,无论我如何更改“ td”百分比,它仍然保持不变。 So I add style="width:100%" into the input type="text"... , then change the "td" width, the "td" width show correct changing. 所以我将style =“ width:100%”添加到输入type =“ text” ...中,然后更改“ td”宽度,“ td”宽度显示正确的更改。 the issue get fixed. 问题得到解决。 Below code is how I correct it. 下面的代码是我如何纠正它。

<input type="text" disabled="disabled" style="width:100%; text-align:center; border-width:0; background-color:transparent" id="column-Quantity-<%: cart.RecordID %>" value="<%: cart.Quantity %>"/> 

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

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