简体   繁体   English

javascript动态更改宽度不成功

[英]javascript dynamically changing width is not successful

I have the following code to change the height/width of the div, but it is not working. 我有以下代码来更改div的高度/宽度,但是它不起作用。 The alert messages below prints only blank values. 下面的警报消息仅打印空白值。 Please help 请帮忙

<script>
    function showmyidlist() {
        try {
            var myid = document.getElementById('myid');
            var myidlist = document.getElementById('myidlist');
            var divinput = myid.getElementsByTagName("input")[0];
            alert(divinput.style.height + ":" + divinput.style.height);
            myid.style.width = divinput.style.width;
            myid.style.height = divinput.style.height;
            alert(myid.style.width + ":" + divinput.style.width);

            if (myidlist.style.position != 'absolute') {
                myid.appendChild(myidlist);
                myid.style.position = 'relative';
                myidlist.style.position = 'absolute';
                myidlist.style.top = '10px';
                myidlist.style.left = '0px';
                myidlist.style.zindex = 1;
                myidlist.style.display = 'block';
            } else if (myidlist.style.position == 'absolute') {
                myidlist.style.position = '';
                myidlist.style.zindex = 0;
                myidlist.style.display = 'none';
                myid.style.position = '';
            }
        } catch (e) {
            alert(e);
        }
    }
</script>


<table>
    <tr>
        <td style="padding:0px 10px 0px 0px;text-align:right;width:20%;">Name</td>
        <td style="padding:0px 0px 0px 0px;text-align:center;width:60%;">
            <div id="myid"><input type="text" name="myid" value="" onclick="showmyidlist()"></div>
        </td>
        <td style="padding:0px 0px 0px 10px;text-align:left;width:20%;">
            <input style="width:200px;" type="submit" class="button" name="submit" value="Add My Id" />
        </td>
    </tr>
</table>
<div id="myidlist" style="display:none;"></div>

Try: 尝试:

alert(divinput.clientHeight + ":" + divinput.clientWidth);

The clientHeight and clientWidth properties are set dynamically, where as the width and height properties are generally what is initially set. clientHeight和clientWidth属性是动态设置的,而width和height属性通常是最初设置的。

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

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