场景:我的用户有自己的个人资料页面,具有不同的背景颜色和字体,我想使用 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" }
关于php - 通过 Ajax jQuery 改变背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4334600/