问题描述
|
我一直在尝试更改从php文件接收的div的内容。我面临的问题是这部分$(this).siblings(\“ div \”)。html(\“ info \”);
它不更新内容。如果没有成功,它会正常工作,然后回电我想念的是什么?
<script type=\"text/javascript\">
$(function(){
$(\"button\").click(function(){
var name =$(this).attr(\'name\');
var info = \'test=\' + name;
var div= name;
$.ajax({
type: \"GET\",url: \"yes.php\",data: info,success:
function(data)
{
$(this).siblings(\"div\").html(data);
}
});
});
});
</script>
<style type=\"text/css\">
.max {
color: #00F;
height: 25px;
}
.recent {
background-color: #0F0;
width: 500px;
height: 24px;
}
div.min{
width: 200px;
height: 100px;
overflow: hidden;
background-color: #F00;
}
</style>
<div>
<div class=\"min\">Whats up dude?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
<button name=\'what\'>Read More</button>
</div>
<br/>
<div>
<div class=\"min\">Whats up dude?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
<button name=\'jack\'>Read More</button>
</div>
解决方法
ajax调用内的
this
指向xhr对象,不再指向$(\"button\")
。将其存储在ajax调用之前的var中,以后再使用:
$this = $(this);
然后:
$this.siblings(\"div\").html(data);
,我不认为ѭ5拥有您认为在回调函数中具有的作用域。尝试改用显式选择器:
$(\".divClass\").siblings(\"div\").html(data);