[英]Javascript not affecting HTML element
這是一個簡單的網頁。 為什么在調整瀏覽器大小時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>
style.width
需要設置一個單位,如px
。
document.getElementById("main").style.width=window.innerWidth + "px";
嘗試這個:
<!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>
將您的行替換為:
document.getElementById("main").style.width=window.innerWidth + 'px';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.