简体   繁体   English

CSS中的`var`在IE中不起作用

[英]`var` in css not working in IE

I have two variables for horizontal and vertical alignment say xPos and yPos and it will assign the value as 0 for it in css like this: 我有两个用于水平和垂直对齐的变量,分别是xPosyPos ,它将在CSS中将其值分配为0,如下所示:

var xPos="left",
    yPos="bottom";

$(".applyCss").css({
    "width":"200px",
    "height":"200px",
    "position":"absolute",
    "background-color":"#E0E0E0",
    [xPos]:0,
    [yPos]:0
});   

This logic is working fine except in IE. 除IE之外,此逻辑工作正常。 It shows 表明

Expected identifier, string or number

I referred many questions in StackOverflow, and i got the answer as "Internet Explorer have troubles with trailing commas in objects and arrays". 我在StackOverflow中提到了许多问题,得到的答案是“ Internet Explorer在对象和数组中尾随逗号有麻烦”。 But i didn't get any solution for this problem 但是我没有解决这个问题

Here is the jsFiddle 这是jsFiddle

(Note: I got a workaround for this problem, but i want to know a solution for this method) (注意:我有解决此问题的方法,但我想知道此方法的解决方案)

That object literal bracket syntax is new to ES6 (also known as ES2015). 该对象文字括号语法是ES6(也称为ES2015)的新增功能。 It'll work in modern browsers like Firefox, Chrome, and Edge, but IE barely achieves ES5 compliance. 它可以在Firefox,Chrome和Edge等现代浏览器中使用,但IE几乎无法达到ES5规范。

Either use Babel for browser compatibility, or use the old trusty syntax: 使用Babel来实现浏览器兼容性,或者使用旧的可信任语法:

var xPos="left",
    yPos="bottom";

var myStyles = {
    "width":"200px",
    "height":"200px",
    "position":"absolute",
    "background-color":"#E0E0E0",
};
myStyles[xPos] = 0;
myStyles[yPos] = 0;
$(".applyCss").css(myStyles);

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

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