gpt4 book ai didi

javascript - 问题?

转载 作者:行者123 更新时间:2023-11-27 22:30:20 25 4
gpt4 key购买 nike

我正在尝试将此 jquery 脚本转换为切换事件类型,这是我的脚本,带有插图,以展示我想要发生的事情。我似乎不太了解它,所以这里是:

//myElement.toggle(vote(), unvote()); // this is the toggle event

$(document).ready(function() {
$('.statuses').delegate('.vote_up', 'click', function(e) {

//stop event
e.preventDefault();
//get the id
var the_id = $(this).closest('.message').attr('id').split('_').pop();

//function vote () // this should be the vote up function
//the main ajax request
$.ajax({
context: this,
type: "POST",
// Make sure "this" in the callback refers to the element clicked

data: "action=vote_up&id=" + the_id,
url: "ajax/votes.php",
success: function (msg) {

$(this).siblings("span.vote_count").html(msg).fadeIn();
// get the child <img> and set its src
$(this).children("img").attr("src", "img/uparrowActive.png");
}
});

//function unvote() thier should be another function here to unvote toggle

$.ajax({
context: this,
type: "POST",
// Make sure "this" in the callback refers to the element clicked

data: "action=vote_down&id=" + the_id,
url: "ajax/votes.php",
success: function (msg) {

$(this).siblings("span.vote_count").html(msg).fadeIn();
// get the child <img> and set its src
$(this).children("img").attr("src", "img/uparrow.png");
}
});
});

我只是不知道如何将这个切换事件集成到这个 jquery 片段中,请有人帮助我,我已经尝试了多年:)) 谢谢

最佳答案

这样做:

$('.statuses').delegate('.vote_up, .vote_down', 'click', function(e) {
if($(this).hasClass('.vote_up') {
src = "img/uparrowActive.png";
action = "vote_up";
}
else {
src = "img/uparrow.png";
action = "vote_down";
}

$.ajax({
context: this,
type: "POST",
// Make sure "this" in the callback refers to the element clicked

data: "action=" + action + "&id=" + the_id,
url: "ajax/votes.php",
success: function (msg) {

$(this).siblings("span.vote_count").html(msg).fadeIn();
// get the child <img> and set its src
$(this).children("img").attr("src", src);
}
});
});

关于javascript - 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3918653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com