jQuery父更改内部HTML
到目前为止,我已经做到了:jQuery父更改内部HTML,jquery,parent,innerhtml,Jquery,Parent,Innerhtml,到目前为止,我已经做到了: $('.event-tools a').click(function() { var status = $(this).attr('id'); var id = $(this).parent().attr('id'); $.ajax({ type: "GET", url: "http://localhost:8888/update/test.php", data: "rsvp=" + stat
$('.event-tools a').click(function()
{
var status = $(this).attr('id');
var id = $(this).parent().attr('id');
$.ajax({
type: "GET",
url: "http://localhost:8888/update/test.php",
data: "rsvp=" + status + '&id=' + id,
success: function()
{
$(this).parent().html(status);
//alert(status);
},
error: function()
{
alert('failz');
}
});
});
为此:
<span class="rsvp" id="1">
<a href="#" class="rsvp" id="Attending">Attending</a>
<a href="#" class="rsvp" id="Maybe Attending">Maybe Attending</a>
<a href="#" class="rsvp" id="Not Attending">Not Attending</a>
</span>
当我单击其中一个并使用
警报()
时,它会提示警报框,但是,当我使用$(this.parent().html(status)时在success:
事件中,它不会更改HTML的状态
。此
在成功函数内部的作用域与外部的作用域不同,因为JavaScript具有函数作用域。您需要执行类似操作以保留外部函数的this
的原始值:
var self = this;
$.ajax({
// ...
success: function() {
$(self).parent().html(status);
}
});
或者,您可以使用jQuery的:
试试这个
JS:
HTML:
不,不会更改内部HTML。现在,我为什么没有想到这一点,代理的工作非常棒。为此干杯!(5分钟后我才能接受答案)。我认为你的身份证不是有效身份证。。。
$.ajax({
// ...
success: $.proxy(function() {
$(this).parent().html(status);
}, this)
});
$('.event-tools a').click(function(ev)
{
ev.stopPropagation()
var status = $(this).attr('id'),
id = $(this).parent().attr('id'),
myparent = $(this).parent();
$.ajax({
type: "GET",
url: "http://localhost:8888/update/test.php",
data: "rsvp=" + status + '&id=' + id,
success: function()
{
$(myparent).parent().html(status);
//alert(status);
},
error: function()
{
alert('failz');
}
});
return false;
});
<span class="event-tools" id="1"><!-- this has class = "rsvp" -->
<a href="#" class="rsvp" id="Attending">Attending</a>
<a href="#" class="rsvp" id="Maybe Attending">Maybe Attending</a>
<a href="#" class="rsvp" id="Not Attending">Not Attending</a>
</span>