I'm trying to create vars for some styles that i want to and reuse, but can't seem to get it to work. What am I doing wrong?

我正在尝试为一些我想要的样式创建vars并进行重用,但似乎无法使其工作。我做错了什么?

var bgColor = "'background' : 'rgb(102,204,0)'";
var textColor = "'color' : 'rgb(40,40,40)'";  

$('.className').css({bgColor, textColor});

4 个解决方案

#1


3

Taking a look at the documents on jquery css if you wanted to apply these values in a single call you'd need to create valid json. Per the documents:

如果您想在单个调用中应用这些值,那么就需要创建有效的json。每个文件:

var validValues = 
{ 
  "background-color": "#ffe", 
  "border-left": "5px solid #ccc" 
};

or

var validValues = 
{
  backgroundColor: "#ffe", 
  borderLeft: "5px solid #ccc" 
} 

then

然后

$(selector).css(validValues);

Notice that with the DOM notation, quotation marks around the property names are optional, but with CSS notation they're required due to the hyphen in the name

注意,使用DOM表示法时,属性名周围的引号是可选的,但是使用CSS表示法时,由于名称中的连字符,它们是必需的

specifically the reason yours does not work is the following does not create valid json for jquery:

具体来说,您的失败原因是以下原因没有为jquery创建有效的json:

var bgColor = "'background' : 'rgb(102,204,0)'";
var textColor = "'color' : 'rgb(40,40,40)'";  

var json = {bgColor, textColor};

json =

json =

{
  bgColor: "'background' : 'rgb(102,204,0)'", 
  textColor: "'color' : 'rgb(40,40,40)'"
}

更多相关文章

  1. 如何在动态创建的HTML标签内容中避免单引号?
  2. php单引号和双引号的区别
  3. 单引号,双引号,javascript,HTML,转义字符
  4. 不使用PHP框架的原因?
  5. 如何调试UPDATE函数在PHP中无法正常工作的原因?
  6. 关于uoloadify不能显示效果原因(thinkphp5仿百度糯米)
  7. fgetcsv()错误地将双引号添加到第一行的第一个元素
  8. 这是什么原因啊,没有找出错来啊(数据库用的是mysql)
  9. 使用IP地址方法登录MySQL数据库Can't connect to MySQL serv

随机推荐

  1. 爬取6271家死亡公司数据,看十年创业公司消
  2. yum的使用详解
  3. 王思聪究竟上了多少次热搜?
  4. 怎样在JavaScript中创建和填充任意长度的
  5. 我用Python展示Excel中常用的20个操作
  6. 在 Node.js 中通过子进程操作标准输入/输
  7. Pandas也能修改样式?快速给你的数据换个St
  8. React 教程:React 快速上手指南 [每日前端
  9. Python办公自动化|自动整理文件,一键完成!
  10. 如何科学的从早起这里薅走一本书