2013-01-24 30 views
0

我在Javascript中有一段代碼來檢測屏幕的分辨率。 現在我的結果(1920 x 1080)站在一個文本框(輸入)內,但我只是希望它沒有文本框。我如何需要這樣做?Shot Javascript <script></script> not in TextArea

代碼:

<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> 
+1

我在這裏看不到一個textarea .. – darma

+0

@darma我猜他的意思了'input's –

+0

我不是確定「站在文本區域內」是什麼意思,你的意思是這些值顯示在你的輸入內部嗎? – jbabey

回答

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

也許嘗試加入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; 
} 
+0

是啊,這對我有用 –

+0

非常好,很高興幫助:) – BenM

+0

你可能想接受,如果它是一個有用的答案。 – BenM

0

如果你在一個工作網絡應用程序陽離子,你可能會對瀏覽器窗口的視口大小更感興趣。這裏是你如何獲得這些信息。如果用戶更改屏幕大小,則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>