[英]How to send json data to jQuery.css()
I want to send json formatted data to jQuery.css() function, and i dont know how to do that. 我想将json格式的数据发送到jQuery.css()函数,但我不知道该怎么做。 Later on, i will send more properties, this is just example of my problem.
稍后,我将发送更多属性,这只是我的问题的示例。
//Sorry, this var x is actually string that i get when i print my json string
var x = {"transform":"rotate(30deg)","-o-transform":"rotate(30deg)"}
//If i try to do something like this wont work
$("#mainImage").css(x);
//but following works
$("#mainImage").css({"transform":"rotate(30deg)","-o-transform":"rotate(30deg)"})
It probably has to do something that jquery accepts .css( map )
它可能必须做些jquery接受
.css( map )
事情
A map of property-value pairs to set. 要设置的属性值对的映射。
And i am trying to send single text string, can i somehow convert json to map ? 我正在尝试发送单个文本字符串,我可以以某种方式将json转换为map吗?
@Pekka Yes, i am sure that $("#mainImage").css(x);
@Pekka是的,我确定
$("#mainImage").css(x);
doesnt work. 不起作用。
@Felix That is what i get by calling json = JSON.stringify(data);
@Felix这就是我通过调用
json = JSON.stringify(data);
得到的json = JSON.stringify(data);
sorry if it not json data, i am js newbie.. 抱歉,如果它不是json数据,我是js新手。
You should parse not stringify JSON before. 您应该先解析不对 JSON进行字符串化处理 。 I tried this one It's works.
我尝试了这个作品。
var json = '{ "display": "none" }';
var cssObject = JSON.parse(json);
$("#mainImage").css(cssObject);
I've just tried with this code: 我刚刚尝试使用此代码:
$(document).ready(function() {
var x = {"background-color": "yellow"};
$('body').css(x);
});
And basically it works. 基本上可以正常工作。 So the problem can be in sth totally different.
因此,问题可能完全不同。 Maybe your img element is not there?
也许您的img元素不存在?
http://jsfiddle.net/A4azg/ http://jsfiddle.net/A4azg/
var cssObj = { 'background-color' : '#ddd', 'font-weight' : '', 'color' : 'red', "transform":"rotate(30deg)", "-o-transform":"rotate(30deg)", "-webkit-transform":"rotate(30deg)" } $("#mainImage").css(cssObj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.