Javascript 如何修改与其他div共享类名的特定div?
所以我有这个html:Javascript 如何修改与其他div共享类名的特定div?,javascript,jquery,ajax,Javascript,Jquery,Ajax,所以我有这个html: <td class="score_section"> <div class="row collapse" align="center"> <div class="upvotebutton" onclick="javascript: Upvote()">▲</div> <div class="votescore"> 40 </div&
<td class="score_section">
<div class="row collapse" align="center">
<div class="upvotebutton" onclick="javascript: Upvote()">▲</div>
<div class="votescore"> 40 </div>
<div class="donwvotebutton" onclick="javascript: Downvote()">▼</div>
</div>
</td>
在成功进行向下投票或向上投票后,使用来自ajax的传入数据刷新特定的“votescore”div(与向上投票/向下投票按钮共同响应的div)
只有当我点击它刷新所有“votescore”div时,一切正常,有什么想法可以解决这个问题吗 您应该给它一个ID,然后使用它刷新div
<td class="score_section" id="section1">
<div class="row collapse" align="center">
<div class="upvotebutton" onclick="javascript: Upvote(section1)">▲</div>
<div class="votescore"> 40 </div>
<div class="donwvotebutton" onclick="javascript: Downvote(section1)">▼</div>
</div>
</td>
您可以绑定侦听器,该侦听器允许您使用
this
$('.upvotebutton').on('click', function(){
var me = $(this);
var vote = +1;
$.ajax({
type: "post",
url: "Ajax/vote-o-matic.php",
data: {
user_id: user_id,
thread_id: thread_id,
vote: vote
},
success: function (res) {
me.parent().find('.votescore').html(res);
}
});
});
使用.parent而不是.siblings的额外步骤
user\u id
和thread\u id
变量来自何处?您的代码(如图所示)将抛出一个未定义的异常。@它们出现在OP的原始代码段中,似乎在其他地方定义。问题是$(this.parent().find('.votescore').html(res);不会刷新页面score@Tiberiu你是说me.parent().find('.votescore').html(res)代码>?
function Upvote(div_id,user_id, thread_id){
var vote = +1;
$.ajax({
type: "post",
..
success: function(res){
$(div_id).html(res);
}
});
}
$('.upvotebutton').on('click', function(){
var me = $(this);
var vote = +1;
$.ajax({
type: "post",
url: "Ajax/vote-o-matic.php",
data: {
user_id: user_id,
thread_id: thread_id,
vote: vote
},
success: function (res) {
me.parent().find('.votescore').html(res);
}
});
});