简体   繁体   English

将Jquery代码转换为插件

[英]Converting Jquery Code To A Plugin

I"m trying to convert a Jquery code to a plugin. I'm having difficulties doing it as it is only working half properly. The code in question makes a textarea get a hover menu which enables the user to append markup tags to the textarea for bold, italics, underline and link. My conversion only makes the hover menu appear, but the actually adding of the markup tags are not working. The code which I'm trying to convert is found here: http://www.jankoatwarpspeed.com/post/2010/05/23/microsoft-office-minibar-jQuery-CSS3.aspx 我正在尝试将Jquery代码转换为插件。由于它只能正常工作一半,因此很难执行。所讨论的代码使textarea拥有一个悬停菜单,该菜单使用户可以将标记标签附加到textarea对于粗体,斜体,下划线和链接,我的转换仅使悬停菜单出现,但是标记标记的实际添加不起作用。我尝试转换的代码位于: http://www.jankoatwarpspeed .com / post / 2010/05/23 / microsoft-office-minibar-jQuery-CSS3.aspx

Here's the whole generic code: 这是整个通用代码:

$(document).ready(function() {

            var mouseX = 0;

            var mouseY = 0;



            $("#description").mousemove(function(e) {

                // track mouse position

                mouseX = e.pageX;

                mouseY = e.pageY;

            });



            $("#description").mousedown(function() {

                $("#menu").fadeOut("1000");

            });



            $("#description").select(function() {

                // get the mouse position an show the menu

                $("#menu").css("top", mouseY - 30).css("left", mouseX + 10).fadeIn("1000");

            });



            $("#bold").click(function() {

                wrapText("<b>", "</b>");

                $("#menu").fadeOut("1000");

            });



            $("#italic").click(function() {

                wrapText("<i>", "</i>");

                $("#menu").fadeOut("1000");

            });



            $("#underline").click(function() {

                wrapText("<u>", "</u>");

                $("#menu").fadeOut("1000");

            });



            $("#link").click(function() {

                var url = prompt("Enter URL", "http://");

                if (url != null)

                    wrapText("<a href='" + url + "'>", "</a>");

                $("#menu").fadeOut("1000");

            });



            function wrapText(startText, endText){

                // Get the text before the selection

                var before = $("#description").val().substring(0, $("#description").caret().start);



                // Get the text after the selection

                var after = $("#description").val().substring($("#description").caret().end, $("#description").val().length);



                // merge text before the selection, a selection wrapped with inserted text and a text after the selection

                $("#description").val(before + startText + $("#description").caret().text + endText + after);



                // Update the preview

                $("#preview").html($("#description").val());

            }

        });

Here's where I"ve gotten in the conversion: 这是我进行转换的地方:

//You need an anonymous function to wrap around your function to avoid conflict
(function($){

    //Attach this new method to jQuery
    $.fn.extend({ 

        //This is where you write your plugin's name
        officebar: function() {

            //Iterate over the current set of matched elements
            return this.each(function() {

                 var mouseX = 0;

            var mouseY = 0;



            $(this).mousemove(function(e) {

                // track mouse position

                mouseX = e.pageX;

                mouseY = e.pageY;

            });



            $(this).mousedown(function() {

                $("#menu").fadeOut("1000");

            });



            $(this).select(function() {

                // get the mouse position an show the menu

                $("#menu").css("top", mouseY - 30).css("left", mouseX + 10).fadeIn("1000");

            });



            $("#bold").click(function() {

                wrapText("<b>", "</b>");

                $("#menu").fadeOut("1000");

            });



            $("#italic").click(function() {

                wrapText("<i>", "</i>");

                $("#menu").fadeOut("1000");

            });



            $("#underline").click(function() {

                wrapText("<u>", "</u>");

                $("#menu").fadeOut("1000");

            });



            $("#link").click(function() {

                var url = prompt("Enter URL", "http://");

                if (url != null)

                    wrapText("<a href='" + url + "'>", "</a>");

                $("#menu").fadeOut("1000");

            });



            function wrapText(startText, endText){

                // Get the text before the selection

                var before = $(this).val().substring(0, $(this).caret().start);



                // Get the text after the selection

                var after = $(this).val().substring($(this).caret().end, $(this).val().length);



                // merge text before the selection, a selection wrapped with inserted text and a text after the selection

                $(this).val(before + startText + $(this).caret().text + endText + after);



                // Update the preview

                //$("#preview").html($(this).val());

            }

            });
        }
    });

//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )       
})(jQuery);

The Menu HTML code: 菜单HTML代码:

<div id="menu">
    <a href="#" id="bold">b</a>
    <a href="#" id="italic">i</a>
    <a href="#" id="underline">u</a>
    <a href="#" id="link">Link</a>
</div>

The Menu CSS: 菜单CSS:

#menu {padding:5px; background-color:#f5f5f5;
       background-color:rgba(245, 245, 245, 0.6);
       display:none; position:absolute; top:0px; left:0px; overflow:hidden;
       border:solid 1px #929292; border-radius:3px; -moz-border-radius:3px;
       -webit-border-radius:3px; box-shadow: 5px 5px 5px #888;
       -moz-box-shadow: 1px 1px 3px #555; -webkit-box-shadow: 5px 5px 5px #888;}
#menu:hover {background-color:rgba(245, 245, 245, 1);}

I'm not sure if the function nested inside the plugin code is working properly though. 我不确定嵌套在插件代码中的功能是否正常运行。 Any idea why? 知道为什么吗?

I think the problem is that inside the "wrapText" function, this won't be what you think it is. 我认为问题是在“ wrapText”函数内部, this与您想的不一样。

Try adding 尝试添加

var self = this;

outside of the "wrapText" function, and then inside "wrapText" use $(self) instead of $(this) . “ wrapText”函数外部 ,然后在“ wrapText”内部使用$(self)代替$(this)

When I do that in your jsFiddle, it seems to work. 当我在您的jsFiddle中执行此操作时,它似乎可以工作。 Define "self" inside the ".each()" body. 在“ .each()”主体内定义“自我”。

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

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