简体   繁体   English

我的代码第一次正确执行,但是第二次却没有正确执行(Javascript)。 似乎变量未更新正确

[英]My code executes correctly the first time, but not the second (Javascript). It seems that a variable is not updated correclty

I am creating a jquery plugin. 我正在创建一个jQuery插件。 Whenever you double click on a tag that has class .editable it will replace it with either a textbox or a textarea. 每当您双击具有.editable类的标签时,它将被文本框或文本区域替换。 When you click outside of the .editable tag then it will write then it will remove the textbox or textarea and write the original content back. 当您在.editable标记之外单击时,它将进行写入,然后将其删除文本框或文本区域,并将原始内容写回。

The first time I do this it works as its supposed to. 我第一次执行此操作时应按预期进行。 However, if I click on a different content field the second time when I click off it is replaced with the content of the editable area that I clicked on the first time. 但是,如果我第二次单击时单击另一个内容字段,则该字段将替换为我第一次单击的可编辑区域的内容。

To see what I'm talking about go to modx.harrishosting.com and doubleclick on home. 要查看我在说什么,请访问modx.harrishosting.com并双击home。 Click on a different part of the webpage and it will return to normal. 单击网页的其他部分,它将恢复正常。 Now, doubleclick on the lerem ipsum content and click on a separate portion of the webpage. 现在,双击lerem ipsum内容,然后单击网页的单独部分。 Instead of going back to normal the Lorem ipsum content is replaced with the home content. Lorem ipsum内容没有恢复正常,而是替换为原始内容。

Here is my code. 这是我的代码。 Please help! 请帮忙!

jQuery.fn.jqfEdit = function(){
    return this.each(function(){

        // Save "this" to "editable" because this changes when scope changes
        var editable = this;

        // Fix to display box-shaddow correctly when editable area is highlighted       
        $(editable).css({'display':'inline-block'});

        // Highlight editable area with box-shaddow on hover
        if (!($(editable).hasClass('.selected'))) {

            // What to do on mouse over
            $(editable).mouseenter(
                function() {
                    $(editable).css({'-moz-box-shadow' : '0px 0px 10px #eec900', '-webkit-box-shadow' : '0px 0px 10px #eec900', 'box-shadow' : '0px 0px 10px #eec900'});
                }

            // What to do on mouse out.
            ).mouseleave(
                function() {
                    $(editable).css({'-moz-box-shadow' : '0px 0px 0px #eec900', '-webkit-box-shadow' : '0px 0px 0px #eec900', 'box-shadow' : '0px 0px 0px #eec900'});
                }
            );
        }

        // Ending the if statement
        else {
        }

        // TO DO: Fix double/double click bug in textarea

        // What to do when double clicking an editable area
        $(editable).dblclick(function() {

            // Give the editable area the class "selected"
            $(editable).toggleClass('selected');

            // What to do if editable area is selected (has "selected" class)
            if ($(editable).hasClass('selected')) {

                // Remove the previously given highlight (box-shaddow) from the editable area
                $(editable).css({'-moz-box-shadow' : '0px 0px 0px #eec900', '-webkit-box-shadow' : '0px 0px 0px #eec900', 'box-shadow' : '0px 0px 0px #eec900'});


                // Take everything inside of editable div and assign it to variable named "content"
                var content = $(editable).html();

                // Get the height and width of editable div.
                var height = $(editable).height();
                var width = $(editable).width();

                // Add a few pixels to the width for textbox
                var textBoxHeight = height+5;
                var textBoxWidth = width+10;

                // If length of content is less than 50 characters then use a textbox
                if (content.length < 50) {
                    $('.selected').html('<form id="myForm"> <input id="jqfEdit-textbox" type="text" style="height:' + textBoxHeight + '; width:' + textBoxWidth + '; padding: 2px;" name="myname" value="' + content + '" /> <input type="submit" value="Ok" /> <input type="button" name="Cancel" value="Cancel" /> </form>');

                    // If you click outside of editable div while textbox is focused then remove the editable area and change back to normal.
                    if ($('#jqfEdit-textbox').focus()) {
                        $('body').click(function() {

                            if ($('.selected').html(content)) {
                                $('.selected').toggleClass('selected');
                            }

                        });
                        $(editable).click(function(event){

                            event.stopPropagation();

                        });

                    }

                }

                // If length of content is more than 50 characters then use a textarea
                else {
                    $('.selected').html('<form id="myForm"> <textarea id="jqfEdit-textarea" type="text" style="height:' + height + '; width:' + width + ';" name="myname">' + content + '</textarea> <input type="submit" value="Ok" /> <input type="button" name="Cancel" value="Cancel" /> </form>');

                    // If you click outside of editable div while textbox is focused then remove the editable area and change back to normal.
                    if ($('#jqfEdit-textarea').focus()) {
                        $('body').click(function() {

                            if ($('.selected').html(content)) {
                                $('.selected').toggleClass('selected');
                            }

                        });
                        $(editable).click(function(event){

                            event.stopPropagation();

                        });

                    }
                }             
            }
        });
    });

};

One possible reason is using specific values for some IDs of HTML you write. 一种可能的原因是对您编写的某些HTML ID使用特定的值。 You may want to append the ID of editable to it or so. 您可能要在其后面附加可编辑的ID。 I haven't gone throughly enough to tell whether this is possibility of two forms at a time in the entire page (even if one hidden with CSS). 我还没有足够详尽地说明这种可能性是否在整个页面中一次出现两种形式(即使CSS隐藏了一种形式)。 If this happens, it won't work from there on. 如果发生这种情况,那将无法正常工作。

This is not the only reason but one possible from very quick read of the code. 这不是唯一的原因,但是从快速阅读代码中就有可能。

I haven't got time to debug it properly, but the two things that jump out at me as possible problems are: 我没有时间适当地调试它,但可能出现的问题让我跳出了两件事:

The closure created here: 此处创建的关闭:

$('body').click(function() {
    if ($('.selected').html(content)) {
        $('.selected').toggleClass('selected');
    }
});

is using the variable content , which was defined outside of the closure, and could cause things to be leftover from the last time the function ran. 正在使用在闭包外部定义的变量content ,这可能导致从上次函数运行起就遗留任何东西。

Also, be careful when adding events within events (the $('body').click line), you may end up with events that fire twice, which when combines with the point above could create the problem you are experiencing. 另外,在事件中添加事件时要小心( $('body').click行),最终可能会触发两次事件,当与上述要点结合使用时,可能会引起您遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM