0

我有一段 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>
4

3 回答 3

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>

然后innerHTML使用 JavaScript 设置:

function scrResolution()
{
    var width = screen.width;
    var height = screen.height;
    document.getElementById("txt_scrWidth").innerHTML = width;
    document.getElementById("txt_scrHeight").innerHTML  = height;
}
于 2013-01-24T13:21:06.667 回答
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>
于 2013-01-24T13:22:11.047 回答
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>
于 2013-01-24T13:32:19.780 回答