简体   繁体   English

jQuery插件-事件无法正确触发

[英]Jquery Plugin - Events not firing correctly

I'm trying to create a simple jquery menu plugin which will popup once a user clicks on a button. 我正在尝试创建一个简单的jquery菜单插件,一旦用户单击按钮,该插件就会弹出。

What i want to achieve is to pass in a data (JSON) object to the plug in, This object will have the Menu text and the corresponding command that should be fired and also the arguments that should be passed to the command which lies in a separate JS file. 我要实现的是将数据(JSON)对象传递给插件,该对象将具有菜单文本和应被触发的相应命令,以及应传递给位于菜单项中的命令的参数。单独的JS文件。

The basic problem that i'm now facing is that , I cant get the menu items to fire the click event which is bind. 我现在面临的基本问题是,我无法获取菜单项来触发绑定的click事件。 It would be great if someone could point me in the right direction. 如果有人能指出我正确的方向,那就太好了。

Plug in Code 插入代码

$.fn.rioContextMenu = function (options) {
    var opts = $.extend( {}, $.fn.rioContextMenu.defaults, options );
    var menu = "<div class='btn-group'><button class='btn dropdown-toggle' data-toggle='dropdown'><span class='glyphicon glyphicon-edit'></span></button><ul class='dropdown-menu'>";

    if (opts.data != null) {
                $.each(opts.data, function(key, value) {
                menu += "<li><a id='"+value.MenuId+ "'>" + value.MenuText + "</a></li>";
                   var menuItem = $("#" + value.MenuId);

                   menuItem.bind('click', function() {
                        alert('test');
                    });​
                });
        menu += "</ul></div>";
        return $(this).html(menu);
}

}(jQuery));

The JSON object which is passed in 传入的JSON对象

var menuData = [{
                            MenuText : 'Edit Document', 
                            MenuCmd: testMethod,
                            MenuId: "editDocumentItem"
                        },
                        {
                            MenuText: "Entered in error",
                            MenuCmd: testMethod,
                            MenuId: "errorItem"
                        },
                        {
                            MenuText: "Lock/Unlock",
                            MenuCmd: testMethod,
                            MenuId: "lockItem"
                        },
                        {
                            MenuText: "History",
                            MenuCmd: testMethod,
                            MenuId: "historyItem"
                        },
                        {
                            MenuText : "Print", 
                            MenuCmd: testMethod,
                            MenuId: "printItem"
                        }];

You can use the id-selector in the document context only after the html content is added to the dom. 只有将html内容添加到dom后,才能在文档上下文中使用id选择器。

In your loop, when you say var menuItem = $("#" + value.MenuId); 在循环中,当您说var menuItem = $("#" + value.MenuId); , you have added the html(string) to the variable menu but it is not yet added to the dom so the selector "#" + value.MenuId will not return any element thus the click handler is not added to the target element. ,您已将html(string)添加到变量menu但尚未将其添加到dom中,因此选择器"#" + value.MenuId将不返回任何元素,因此单击处理程序未添加到目标元素。

One option here is to first add the content to the dom, then add the click handler like 这里的一个选项是先将内容添加到dom,然后添加点击处理程序,例如

 (function($) { $.fn.rioContextMenu = function(options) { var opts = $.extend({}, $.fn.rioContextMenu.defaults, options); var menu = "<div class='btn-group'><button class='btn dropdown-toggle' data-toggle='dropdown'><span class='glyphicon glyphicon-edit'></span></button><ul class='dropdown-menu'>"; if (opts.data != null) { $.each(opts.data, function(key, value) { menu += "<li><a id='" + value.MenuId + "'>" + value.MenuText + "</a></li>"; }); menu += "</ul></div>"; var $html = $(menu); $html.find('a').click(function() { alert('test'); }); return $(this).html($html); } }; })(jQuery); jQuery(function($) { var testMethod = {}; var menuData = [{ MenuText: 'Edit Document', MenuCmd: testMethod, MenuId: "editDocumentItem" }, { MenuText: "Entered in error", MenuCmd: testMethod, MenuId: "errorItem" }, { MenuText: "Lock/Unlock", MenuCmd: testMethod, MenuId: "lockItem" }, { MenuText: "History", MenuCmd: testMethod, MenuId: "historyItem" }, { MenuText: "Print", MenuCmd: testMethod, MenuId: "printItem" }]; $('#ct').rioContextMenu({ data: menuData }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ct"></div> 

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

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