当试图访问.box的类$容器,使用(这个)内的Ajax调用不起作用。
When attempting to access the '.box' class of the $container, using (this) inside of the ajax call doesn't work.
$container.on(
"click",
".box",
function(event){
var description;
if($(this)[0].style.width == '70%'){
$(this).find(".resultData").fadeOut('slow');
$(this).css('width', '18%');
}else{
$.ajax({
url:'scripts/php/fetchResultsData.php',
data: {action:value},
type: 'post',
dataType: 'json',
success: function(data){
description = data[0];
$(this).css('width', '70%');
$(this).append("\
<div class='resultData'>\
<div class='resultName'>" + $(this).find("p").html() + "</div>\
<div class='resultDesc'>" + description +"</div>\
</div>");
/*alert($(this).find("p").html());*/
}
})
}
$container.masonry('reload');
}
);
在情况下,它并不清楚我想要做的,我试图改变动态元素的CSS。但是,例如,
In case it's not clear what I'm trying to do, I'm attempting to change the css of dynamic elements. But for example,
$(本)的CSS('宽','70%');
是不是调整的CSS的。如果我移动了ajax,成功的部分之外,它的工作原理,但我无法得到描述。
Isn't adjusting the css at all. If I move it outside the ajax,success section, it works, but then I'm unable to get 'description'.
您正在接近。 '这'在你使用它的上下文中,指的是Ajax请求,而不是发出该事件的事。为了解决这个问题,使得AJAX请求之前保存一份本:
You're close. 'this' in the context you are using it, refers to the ajax request rather than the thing that issued the event. To fix this, store a copy of this before making the ajax request:
}else{
var me = this;
$.ajax({
...
success: function(data){
description = data[0];
$(me).css('width', '70%');