当尝试访问 $container 的“.box”类时,在 ajax 调用内部使用 (this) 不起作用。
$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。但例如,
$(this).css('width','70%');
根本不调整css。如果我将其移到 ajax,success 部分之外,它会起作用,但随后我无法获得“描述”。
你很接近了。在您使用它的上下文中,“this”指的是 ajax 请求,而不是发出事件的事物。要解决此问题,请在发出 ajax 请求之前存储此副本:
}else{
var me = this;
$.ajax({
...
success: function(data){
description = data[0];
$(me).css('width', '70%');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)