简体   繁体   English

如何将变量传递给javascript中的样式属性?

[英]How to pass a variable to the style property in javascript?

I'm sorry if the question is phrased with the incorrect terminology.如果问题使用了不正确的术语,我很抱歉。

Basically I want to specify what style I want to change in a function by passing it a variable.基本上我想通过传递一个变量来指定我想在函数中改变什么风格。

function styleChanger(ele, styleProp, x){
      ele.style.styleProp = x + "px";
}

var box = document.getElementById("box");
var height = 100;
var width = 100;
var top = 500;
var styleProp = [height,width,top];

styleChanger(box, styleProp[0], height);
styleChanger(box, styleProp[1], width);
styleChanger(box, styleProp[2], top); 

I want to do this but its not possible, is there a way?我想这样做,但这是不可能的,有办法吗?

On this line:在这一行:

var styleProp = [height,width,top];

I think you meant:我想你的意思是:

var styleProp = ["height","width","top"];

If so: In JavaScript, you can refer to a property either using dotted notation and a literal property name ( obj.foo ), or using bracketed notation and a string property name ( obj["foo"] ).如果是这样:在 JavaScript 中,您可以使用点符号和文字属性名称 ( obj.foo ) 或使用方括号符号和字符串属性名称 ( obj["foo"] ) 来引用属性。 In the latter case, of course, you can use any expression that evaluates to a string, including a variable reference.当然,在后一种情况下,您可以使用任何计算结果为字符串的表达式,包括变量引用。

So within styleChanger , you can use bracketed notation:因此,在styleChanger ,您可以使用括号表示法:

ele.style[styleProp] = x + "px";
//        ^------- Not the same `styleProp` as above; this one is
//                 the arg to the `styleChanger` function

Because of the styleProp confusion, here's a complete example with the name of the array changed:由于styleProp混淆,这里有一个完整的示例,其中数组的名称已更改:

function styleChanger(ele, styleProp, x){
      ele.style[styleProp] = x + "px";
      //       ^----------- bracketed notation
}

var box = document.getElementById("box");
var height = 100;
var width = 100;
var top = 500;
var styleNames = ["height","width","top"];

styleChanger(box, styleNames[0], height);
styleChanger(box, styleNames[1], width);
styleChanger(box, styleNames[2], top); 

使用括号表示法:

ele.style[styleProp] = ...

You can use an object instead :您可以改用对象:

 var box = document.getElementById("box");
 var styleProp = {"height": 100, "width": 100, "top": 500};

 for(var key in styleProp) {
    styleChanger(box, key, styleProp[key]);
 };  

 function styleChanger(ele, key, styleProp){
    ele.style[key] = styleProp + "px";
 }

Like @tymeJV and @TJCrowder told you, you have to use bracketed notation.就像@tymeJV 和@TJCrowder 告诉您的那样,您必须使用括号表示法。 But i've found some bugs in your code.但是我在您的代码中发现了一些错误。 You shall not use the word "top" as a variable name.不得将“top”一词用作变量名。 This may cause erros in some browsers.这可能会导致某些浏览器出错。 If you use console.log over your "top" variable in chrome, it will log "Window" not "500" as expected.如果您在 Chrome 中的“top”变量上使用 console.log,它将按预期记录“Window”而不是“500”。 Also, in the "styleProp" variable, you should use quotation marks, or else, your array will have numbers inside (from your vars) not strings.此外,在“styleProp”变量中,您应该使用引号,否则,您的数组将包含数字(来自您的变量)而不是字符串。 So, your code should be like that:所以,你的代码应该是这样的:

function styleChanger(ele, styleProp, x){
    ele.style[styleProp] = x + "px";
}

var box = document.getElementById("box");
var height = 100;
var width = 100;
var _top = 500;
var styleProp = ["height","width","top"];

styleChanger(box, styleProp[0], height);
styleChanger(box, styleProp[1], width);
styleChanger(box, styleProp[2], _top); 

I just wanted to add another way of approaching this, as well as support R3tep's answer of storing the style changes in an object:我只是想添加另一种方法来解决这个问题,并支持 R3tep 将样式更改存储在对象中的答案:

let styleChanges = {
  "#box": {
      "height": "100px",
      "width": "100px",
      "top": "500px",
      "fontFamily": "inherit"
  }
}

let makeStyleChanges = (item, styleProps) => {
  for(let key in styleProps) {
      item.style[key] = styleProps[key];
    }
}

for (let key in styleChanges){
  let item = document.querySelector(el);
  if(item) {
    makeStyleChanges(item, styleChanges[key]);
  }
}

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

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