jquery - 这个 jquery 脚本有什么问题?

标签 jquery html

这个 jquery 脚本有什么问题?它似乎无法正常运行:没有语法错误(我检查过)。

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

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

            var parent = $(this).closest('.statuses'),
                vote = (($(this).hasClass("vote_up")) ? "up" : "down");

            if(parent.data("vote") == vote) {
                return true; // If already voted up, do nothing, if already voted down, do nothing...
            }

            if(vote == "down") { // Vote down
                src = "img/uparrow.png";
                action = "vote_down";
            }
            else if(vote == "up") { // Vote up
                src = "img/uparrowActive.png";
                action = "vote_up";
            }

            // Setting current vote (up or down)
            // So next time you toggle, we have the trace of the previous vote (up/down)
            // And authorise only do the opposite
            parent.data("vote", vote);

                    $.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);
                        }
                    });
                });
           });

html:

<ul class="statuses">
<li id="set_41" class="message">
 <span class="vote_count">0</span>
 <a href="#" class="vote_up"><img src="img/uparrow.png" /></a>

最佳答案

您向 .delegate() 传递了太多参数

我假设您正在尝试执行以下操作:

$('.statuses').delegate('.vote_up,.vote_down', 'click', func...
<小时/>
$(document).ready(function() {
    $('.statuses').delegate('.vote_up,.vote_down', 'click', function(e) {
        e.preventDefault();
        var the_id = this.parentNode.id.split('_')[1];  // get the id
        var $img = $(this).children('img');      // get the child image
        var src = $img.attr('src');      // get the src of the image
         // it has been clicked if the src contains the word "Active"
        var hasBeenClicked = src.indexOf('Active') > -1;
        var action = this.className;  // action is the same as the className
          // if it has been clicked, reverse the action
        if( hasBeenClicked ) {
            action = ( action === 'vote_up' ) ? 'vote_down' : 'vote_up';
        }
        $.ajax({
            context: this,
            type: "POST",
            data: "action=" + action + "&id=" + the_id,
            url: "ajax/votes.php",
            success: function(msg) {

                $(this).siblings("span.vote_count").html(msg).fadeIn();
                    // set the src of the image
                    // If it has been clicked, remove the word "Active"
                    // If not, add "Active"
                $img.attr("src", function(i,src) {
                    return ( hasBeenClicked ) ? src.replace('Active', '') : src.replace('.png', 'Active.png');
                });
            }
        });
    });
});​

关于jquery - 这个 jquery 脚本有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3919342/

相关文章:

jQuery 工具提示插件 qtip2 - 如何定义像 qtip1 这样的自定义样式?

javascript - 根据值更改 html 5 输出对象的颜色

c# - 尝试在 MVC 4 Web 应用程序中将页脚置于容器之外

java - 覆盖标签高度元素

javascript - JQuery 似乎不适用于服务器控制

javascript - 测试 JavaScript 的最佳方法是什么?

javascript - 在 Canvas 上绘图时 "Uncaught TypeError: Cannot read property ' getContext ' of undefined"

javascript - 在 jQuery 中删除选中的复选框

javascript - jquery 对话框 - 希望弹出窗口内的单选按钮能够输入值

c# - WebBrowser 控件 c# Scrollbars