Php 使用ajax-JQuery更改div背景

Php 使用ajax-JQuery更改div背景,php,jquery,Php,Jquery,我有一个网页与html如下 <head> <script type="text/javascript" src="jquery-1.5.1.js"></script> <style type="text/css"> #content{ width: 200px; height: 200px; background-image: url(images/loader.gif); background-position: center;

我有一个网页与html如下

<head>
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<style type="text/css">
#content{
  width: 200px;
  height: 200px;
  background-image: url(images/loader.gif);
  background-position: center;
  background-repeat: no-repeat;
  border: #3399FF solid 2px;
}
</style>
</head>
<body>
<div id="content">stringname</div>
</body>
我怎样才能做这个JQuery

提前谢谢

布拉斯拉弗雷德

$.ajax({
    ...ajax stuff...
    url : 'urlToYourPhpFile',
    data : {
        whateverTheHellYouWannaSendInAnObjectLiteralForm : true,
        floop : 'indeed'
    },
    type: 'POST, GET...',
    success: function(url) {
        ...your usual stuff
        $('#content').css('backgroundImage', url);
    }
});

Query假设Ajax .PHP在DIV中准确地返回你想要的数据。如果数据继续很多值,而你只想在div中指定一个特定的值,比如标题,请考虑在返回字符串上让Ajax .PHP做一个JSONEnEnCODE()。这将简化javascript内部的解析和处理

json_encode() // Call this when you want to populate $.get( 'ajax.php', function(data) { if( data ) { $("#content").html( data ); } } ); //当您想要填充时调用此函数 $.get('ajax.php',函数(数据){ 如果(数据){ $(“#content”).html(数据); } } );

$(文档).ready(函数(){
$cell=$(“#content”);
$.ajax({
类型:“POST”,
url:“ajax.php”,
数据:{
文件名:$(“#内容”).html()
},
成功:功能(响应){
警报(响应);
$cell.css(“背景图像”,“url('/images/“+response+”.jpg')”);
}
});
});
不要忘记将jQuery脚本标记添加到-body-tag的底部(但仍在其中):



这是什么ajax技术??我是一个初学者,你能写完整的代码吗@blasteralfred编辑以显示它的模拟表达式…访问dock以获得大量示例的真正详细描述。我不知道你到底想做什么,想怎么做,所以我只能提供这些。嗨,我添加了一个改变背景的建议。。。看看……)@blasteralfred-现在看一下,它应该很好:)另外,如果您使用Chrome,您可以在开发工具中监视来自ajax.php的响应,或者使用Firefox的Firebug。如果您对此仍有问题,请向我们报告!不工作,伙计…:(你能帮我一个忙吗??你能写一个警告AJAX返回数据的小提琴吗?”Dvir,你可以考虑添加<代码> var $Cys= $(‘α’内容’)吗?< <代码> > $Cys.Css(“Buff-Teal-IMAGE”,“URL(图像/加载器.gif));< /Calp>到你的解决方案??@ BraseRalfRe:更新答案,这里是JSFIDLE(注意URL是)/”为了不出现404错误…,请执行以下操作: // Call this when you want to populate $.get( 'ajax.php', function(data) { if( data ) { $("#content").html( data ); } } );
<script type="text/javascript">
    $(document).ready(function(){
      $cell = $('#content');
      $.ajax({
        type: "POST",
        url: "ajax.php",
        data: { 
            filename: $("#content").html()
        },
        success: function(response){
                    alert(response);
            $cell.css("background-image", "url('/images/" + response + ".jpg')");
        }
      });
    });
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>