不在TextArea中的Javascript <script> </script>

时间:2013-01-24 13:17:23

标签: javascript textbox

我在Javascript中有一段代码来检测屏幕的重新生成。 现在我的结果(1920 x 1080)站在一个文本框(输入)中,但我只想要它没有文本框。我该如何做到这一点?

代码:

<head>

<script type="text/javascript" language="javascript">
    function scrResolution() {
        var width = screen.width;
        var height = screen.height;
        document.getElementById("txt_scrWidth").value = width;
        document.getElementById("txt_scrHeight").value = height;
    }
</script>
    </head>


<body onload="scrResolution();">
  <table width="200" border="0">
     <tr>
       <td>  
 Width :
<input name="txt_scrWidth" type="text" id="txt_scrWidth" size="5" />
      </td>
     </tr>
     <tr>
       <td>
           Height :  
 <input name="txt_scrHeight" type="text" id="txt_scrHeight" size="5" />
       </td>
     </tr>
    </table>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

也许尝试添加span代替:

<table width="200" border="0">
    <tr>
        <td>Width: <span id="txt_scrWidth"></span></td>
     </tr>
     <tr>
         <td>Height: <span id="txt_scrHeight"></span></td>
     </tr>
</table>

然后使用JavaScript设置innerHTML

function scrResolution()
{
    var width = screen.width;
    var height = screen.height;
    document.getElementById("txt_scrWidth").innerHTML = width;
    document.getElementById("txt_scrHeight").innerHTML  = height;
}

答案 1 :(得分:2)

<head>

<script type="text/javascript" language="javascript">
    function scrResolution() {
        var width = screen.width;
        var height = screen.height;
        document.getElementById("txt_scrWidth").innerHTML = width;
        document.getElementById("txt_scrHeight").innerHTML= height;
    }
</script>
    </head>


<body onload="scrResolution();">
  <table width="200" border="0">
     <tr>
       <td>  
 Width : <span id="txt_scrWidth"></span>
      </td>
     </tr>
     <tr>
       <td>
           Height :  <span id="txt_scrHeight"></span>  
       </td>
     </tr>
    </table>
</body>
</html>

答案 2 :(得分:0)

如果您正在处理Web应用程序,则可能对浏览器窗口的视口大小更感兴趣。以下是获取该信息的方法。如果用户更改屏幕大小,onresize侦听器将会更新它。

这是脚本

<script>
//make sure we update if the user resizes the browser
window.onresize = function() {
    showSize();   
};

function showSize() {
    document.getElementById("heightDisplay").innerHTML = document.height;
    document.getElementById("widthDisplay").innerHTML = document.width;
}

showSize();
</script>

这是HTML

<div>
    Height: <span id="heightDisplay">Calculating...</span><br/>
    Width: <span id="widthDisplay">Calculating...</span>
</div>