Applying json data to format style via jQuery
I have an odd problem with the following:
function loadTextbox(jsonUrl,divId){
$.getJSON(jsonUrl, function(json) {
$('#' + divId).html('<h2>'+json.heading+'</h2>');
alert(json.config.headingConfig);
$('#' + divId).children().css(json.config.headingConfig);
})
}
The alert above returns: {color: 'white', fontFamily:'Arial, Times, serif'} However, the format of the text does not change.
Now here is the odd part: If I do this:
function loadTextbox(jsonUrl,divId){
$.getJSON(jsonUrl, function(json) {
$('#' + divId).html('<h2>'+json.heading+'</h2>');
alert(json.config.headingConfig);
$('#' + divId).children().css({color: 'white', fontFamily:'Arial, Times, serif'});
})
}
It works fine... The text format is Arial font and white. I am baffled... 开发者_Python百科which probably means there is a very simple answer, any ideas?
It looks like your json.config.headingConfig
contains a JSON string.
You need to convert it to an actual object by calling $.parseJSON
.
精彩评论