将宽度从百分比转换为像素 [英] Converting width from percentage to pixels

查看:1073
本文介绍了将宽度从百分比转换为像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在< div> 内有一个表格:

I have a table inside a <div>:

<div id="fixeddiv">
    <table id="fixedtable">
        <tr class="firstrow">
            <td class="td11"></td>
            <td class="td12"></td>
            <td class="td13"></td>
        </tr>

        <tr class="secondrow">
            <td class="td21" style="width:10%"></td>
            <td class="td22" style="width:20%"></td> 
            <td class="td23" style="width:70%"></td>
        </tr>
    </table>
</div>

CSS:

CSS:

#fixeddiv
{
    overflow:auto;
    margin:0px;
    padding:0px;
    position: relative;
    text-align:left;
    width: 48%;
}

#fixedtable
{
    background-color: White;
    border-spacing:0px;
    cursor: pointer;
    width: 100%;
    height: 100%;
    font-family: Calibri !important;
    color: Black;
    font-size: 14px;
}

.firstrow
{
    position: absolute;
    margin: 0px;
    left: 0;
    top: 0;
    background: url(../Content/Images/header.jpg) repeat-x center top;
    color: White;
    font-weight: bold;
    text-align: center;

}
#fixedtable tr td
{
    padding: 5px !important;
    border: 1px solid #FFFFFF;
    text-align: center;
}

我正在计算 td21 $('。td21')。width()并将宽度分配给 td11 ,比如 code> $( 'TD11')。宽度($(。TD21' )。宽度())。

I am calculating the width of td21 with $('.td21').width() and assigning the width to td11 like $('td11').width($('.td21').width()).

问题是应用的宽度不一样,它们相差 1px ,我找不到这是怎么回事发生 1px 差异。 .td21 宽度 1px 大于 .td11

The problem is that the widths applying are not same, they vary by 1px and I could not find how this 1px difference occur. The .td21 width is 1px greater than .td11.

任何一个人都可以帮我找到解决方案吗?

Can any one please help me to find the solution?

推荐答案

<div id="div-1" style="width: 1000px;">
    <div id="div-2" style="width: 10%;"></div>
</div>

<script language="javascript">                
    var percents = parseInt(document.getElementById("div-2").style.width);
    var parentWidth = parseInt(document.getElementById("div-1").style.width);
    var pixels = parentWidth*(percents/100);
    alert(pixels); // will print "100"
</script>

这篇关于将宽度从百分比转换为像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆