我有一段重复的代码。
<div class='commentfloatleft'>
<button type='button' class='button2' onclick='upvote($id8)'>
<img src='images/likebutton.png' class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/>
</button>
<button type='button' class='button2' onclick='downvote($id8)'>
<img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/>
</button>
</div>
当我点击 onclick 进行投票时,代码是这样的:
function upvote(box){
$(this).siblings('.upvoteimage').load('upvote.php?box=' + box);
}
我已经尝试了一切方法来让它发挥作用。获取功能正在运行,但我看不到“.upvoteimage”中的任何变化。有谁知道如何解决这个问题。帮助将不胜感激。谢谢。
最佳答案
尝试这种方式:为“button”元素设置一个“upvote”类:
<div class='commentfloatleft'>
<button type='button' class='button2 upvote' id='$id8'>
<img src='images/likebutton.png' class='upvoteimage' width='12' height='12' alt='PearlSquirrel'/>
</button>
<button type='button' class='button2' onclick='downvote()'>
<img src='images/unlikebutton.png' width='12' height='12' alt='PearlSquirrel'/>
</button>
</div>
并使用这个 JS 代码:
$(".upvote").on("click",function(){
var box=$(this).attr("id");
$(this).find('.upvoteimage').attr("src",'upvote.php?box=' + box);
return false;
});
关于javascript - 使用 Jquery 从页面上的许多重复类中获取特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12242515/