简体   繁体   English

Javascript getElementById('id')。style VS var x

[英]Javascript getElementById('id').style VS var x

it seems odd to me that using document.getElementById('myImg').style.width instead of var x = document.getElementById('myImg').style.width yields different results can anyone explain please? 在我看来,使用document.getElementById('myImg')。style.width而不是var x = document.getElementById('myImg')。style.width会产生不同的结果,有人可以解释吗? here's my example code (below, the real code is much longer of course), I'm looking to reduce it as much as I can by storing document.getElementById('myImg').style.width as a variable but upon loading the page it seems to work counter-intuitively... 这是我的示例代码(下面,实际代码当然要更长一些),我希望通过将document.getElementById('myImg')。style.width存储为变量来尽可能地减少它,但是在加载页面似乎与直觉相反...

<img id="myImg" src="w3javascript.gif" style="width:100px;height:132px;">

<script>
window.onload = function myFunction()
{
var x = document.getElementById('myImg').style.width;
    var width = window.innerWidth || document.documentElement.clientWidth;
if(width >= 450){
x = "450px"
}
}
</script>

Now the above code produces different results than this (and I have no idea why...): 现在,上面的代码产生的结果与此不同(并且我不知道为什么...):

<img id="myImg" src="w3javascript.gif" style="width:100px;height:132px;">

<script>
window.onload = function myFunction()
{
var x = document.getElementById('myImg').style.width;
    var width = window.innerWidth || document.documentElement.clientWidth;
if(width >= 450){
document.getElementById('myImg').style.width = "450px"
}
}
</script>

When you do : 当您这样做时:

var x = document.getElementById('myImg').style.width;
var width = window.innerWidth || document.documentElement.clientWidth;
if(width >= 450){
  x = "450px"
}

you in fact first assign the width of your element to the variable x, then you overwrite x with the value "450px", but you don't change the element. 实际上,您实际上是先将元素的宽度分配给变量x, 然后用值“ 450px”覆盖x,但是您无需更改该元素。

In the second example, document.getElementById('myImg').style.width = "450px" actually sets the width of the element, but doesn't change the variable x. 在第二个示例中, document.getElementById('myImg').style.width = "450px"实际上设置元素的宽度,但更改变量x。

To simplify your code, you can use : 为了简化代码,您可以使用:

var x = document.getElementById('myImg');
var width = window.innerWidth || document.documentElement.clientWidth;
if(width >= 450){
  x.style.width = "450px"
}

Assigning a value to a variable never changes the value of another variable or property (exceptions: global scope, with ). 为变量分配值永远不会更改另一个变量或属性的值(例外:全局范围, with )。

Simpler example: 比较简单的例子:

var foo = 42;
var bar = foo;

bar = 21;
// foo is still 42

In the first case, you are only changing the value of variable x . 在第一种情况下,您仅更改变量x的值。
In the second case you are changing the value of the property width . 在第二种情况下,您将更改属性width的值。

JavaScript is pass-by-value , not pass-by-reference . JavaScript是按值 传递 ,而不是按引用传递


If you want to avoid querying for the element twice, either store the element or its style in a variable. 如果要避免两次查询元素,请将元素或其样式存储在变量中。 For example: 例如:

var style = document.getElementById('myImg').style;
// ...
style.width = ...;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM