Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得td的大小_Javascript_Html_Css - Fatal编程技术网

Javascript 如何获得td的大小

Javascript 如何获得td的大小,javascript,html,css,Javascript,Html,Css,我有一个表,其中有td。当我在文本字段中单击时,该字段有onkeypress 数据库中的事件选择值显示一个表,其中我选择值。此选择表位于div中,位置固定。但它也会改变高度 <td class="value">vehicle </td><td> <input type="text" id="txt_sh_vid" onKeyPress="vhc_record()" maxlength="4"> <div id="div_vhc" c

我有一个表,其中有td。当我在文本字段中单击时,该字段有onkeypress 数据库中的事件选择值显示一个表,其中我选择值。此选择表位于div中,位置固定。但它也会改变高度

<td class="value">vehicle </td><td> 
 <input type="text" id="txt_sh_vid" onKeyPress="vhc_record()" maxlength="4">
  <div id="div_vhc"  class="search_form">
</div>


 <input type="text" id="vid" style="display:none;">
这是上表的一部分

    td.value
    {
    background-color:#00628B;
    color:#E6E6DC;
    height:50;

    }

    div.search_form
    {
    position:fixed;
     background-color:white;

}
当我按下textfield中的键时,它也会更改class=value的高度 like div id=div_vhc,而其高度为50

步骤1:添加表格布局:固定到表格元素样式

步骤2:添加溢出:隐藏到TD元素样式

下面是一个示例,其中内部DIV比包含的TD高,但TD将保持在50并隐藏其余部分:

<table style="table-layout:fixed">
    <tr>
        <td style="overflow:hidden;height:50px;border:solid 1px #000">
            <div style="height:100px;background:#f00">Hello<br>I am a very<br>very<br>very<br>very<br>long<br>multiline<br>text...</div>
        </td>
    </tr>
</table>
如果您想让它滚动,请使用td元素样式中的overflow:scroll。让我知道这是否是您正在寻找的:

我猜你想要的是抓住td.value宽度和高度。您可以使用offsetHeight或offsetWidth

我不太确定您想做什么,但是要获得td.value的高度,您可以根据html的结构执行以下假设。当然,如果您希望遍历所有td元素并找到包含类名值的元素,则必须使用正则表达式将元素与其类中的值进行匹配:

您的vhc_录制功能已MIDI:

var myvalue = document.getElementsByTagName('td')[0];  //grabs the td.value element based on your html markup

document.getElementById('div_vhc').style.height = myvalue.offsetHeight+'px';  //sets div_vhc height to that of td.value
document.getElementById('div_vhc').style.width= myvalue.offsetWidth+'px';//sets div_vhc width to that of td.value
我对html和css所做的更改,并添加了一些Visibly属性,使示例看起来更清晰:

<table><tr><td class="value">vehicle </td></tr></table>
<input type="text" id="txt_sh_vid" onKeyPress="vhc_record()" maxlength="4">
<div id="div_vhc"  class="search_form">
</div>


<input type="text" id="vid" style="display:none;">

td.value
{
    background-color:#00628B;
    color:#E6E6DC;
    height: 50px;   
    width: 50px;
}

#div_vhc
{
    position:fixed;
    background-color:white;
    display: none;
    border: 1px solid black;
}

你的实际问题是什么?@sadi这是你的全部代码吗?javascript部分在哪里?@kjy其javascript代码“function vhc_record{var data='vhc='+document.getElementById'txt_sh_vid'.value;loadXMLDoc'ship/vehicle_ship/,'div_vhc',data;document.getElementById'div_vhc'.style.visibility=visible;}“@Sadi,当有人按文本字段上的键时,你基本上想改变td的高度大小,对吗?”@Sadi,那么问题是什么?您希望td.value高度始终为50px?
<table><tr><td class="value">vehicle </td></tr></table>
<input type="text" id="txt_sh_vid" onKeyPress="vhc_record()" maxlength="4">
<div id="div_vhc"  class="search_form">
</div>


<input type="text" id="vid" style="display:none;">

td.value
{
    background-color:#00628B;
    color:#E6E6DC;
    height: 50px;   
    width: 50px;
}

#div_vhc
{
    position:fixed;
    background-color:white;
    display: none;
    border: 1px solid black;
}