微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过Ajax jQuery更改背景颜色?

场景:我的用户有他们自己的个人资料页面,它们具有不同的背景颜色和字体,我想例如使用ajax从某个用户那里检索颜色.即

$.ajax({ 
    type: "POST", 
    data: "id", 
    url: "ajax/css.PHP", 
    success: function (bg,font) { 
        $('#bg').css('background-color', 'bg');
        $('#font').css('font-color', 'font');
    } 

ajax / css.PHP页面

<?PHP

//retrieve the background and font data from database for the id(userID).

// this is the bit I'm stuck here, shall I echo the results or return them :~

?>

解决方法:

JSON在这里可能最简单,像这样:

$.ajax({ 
   type: "POST", 
   data: { id: someIDVariable }, 
   url: "ajax/css.PHP", 
   success: function (result) { 
     $('#bg').css('background-color', result.bg);
     $('#font').css('font-color', result.font);
   } 
});

或者使用$.getJSON()的较短形式是GET选项:

$.getJSON("ajax/css.PHP", { id: someID }, function (result) { 
  $('#bg').css('background-color', result.bg);
  $('#font').css('font-color', result.font);
});

然后在PHP中:

eacho json_encode(array('font'=>$font,'bg'=>$bg));
//which will echo this format: { "font": "Arial", "bg": "#000000" }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐