数学运算符不在window.innerWidth上工作

时间:2014-11-19 09:50:48

标签: javascript jquery

当我使用带有window.innerWidth的数学运算符时,它返回NaN。

我只是尝试将数字1添加到window.innerWidth 我的代码出了什么问题?

  <body>
   <p>Click the button to display this window's height and width (NOT including toolbars and scrollbars).</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var w = window.innerWidth;
    var h = window.innerHeight;
    x = document.getElementById("demo");
    x.innerHTML = "Width: " + w-1 + " Heigth: " + h;
}
</script>
  </body>

View sample at Plunker

1 个答案:

答案 0 :(得分:0)

将您的innerHTML代码更改为

x.innerHTML = "Width: " + (w-1) + " Heigth: " + h;

修改 javaScript 代码,如下所示

以下是您修改后的Plunker

function myFunction() {
    var w = window.innerWidth;
    var h = window.innerHeight;
    x = document.getElementById("demo");
    x.innerHTML = "Width: " + (w-1) + " Heigth: " + h;
}

StackOverflow reference for '+' operator