0

我有以下代码来更改 div 的高度/宽度,但它不起作用。下面的警报消息仅打印空白值。请帮忙

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

1 回答 1

1

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.

于 2013-03-19T15:24:09.200 回答