Javascript不影响HTML元素

时间:2013-12-11 02:17:27

标签: javascript html css

这是一个简单的网页。为什么在调整浏览器大小时textarea的宽度不会改变?

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
    function resize() {
        document.getElementById("main").style.width=window.innerWidth;
    }
    window.onresize = resize;
    window.onload = resize;
</script>
</head>
<body style="margin:0; padding:0;">
<textarea id="main" style="margin:0; padding:0; border:0;" >It was a dark and stormy night, and the rain came pouring down... </textarea>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

style.width需要设置一个单位,例如px

document.getElementById("main").style.width=window.innerWidth + "px";

答案 1 :(得分:1)

试试这个:

<!DOCTYPE html>
<html>
<head>
</head>
<body style="margin:0; padding:0;">
<textarea id="main" style="margin:0; padding:0; border:0;" >It was a dark and stormy night, and the rain came pouring down... </textarea>
<script type="text/javascript">
    function resize() {
        document.getElementById("main").style.width=window.innerWidth + 'px';
    }
    window.onresize = resize;
    window.onload = resize;
</script>
</body>
</html>

答案 2 :(得分:1)

将您的行替换为:

document.getElementById("main").style.width=window.innerWidth + 'px';