简体   繁体   English

AJAX中的jQuery上下文菜单加载了内容

[英]jQuery context menu in AJAX loaded content

Well, after hours of trying to find something out, I have to ask here and probably again (I have found similar questions, but none of them helped me). 好了,经过数小时的尝试,我不得不在这里又可能又要问一次(我发现了类似的问题,但没有一个对我有帮助)。

I am trying to use custom context menu on page that is dynamicly loaded via AJAX - everything with jQuery. 我正在尝试使用通过AJAX动态加载的页面上的自定义上下文菜单-jQuery的所有功能。 Problem is that the context menu just does not work on dynamicly loaded page...no menu is shown after right click at all. 问题是上下文菜单仅在动态加载的页面上不起作用...右键单击后根本没有显示菜单。

I have already found out that using .live() is a solution, but still can't get it working. 我已经发现使用.live()是一种解决方案,但仍然无法使其正常工作。 Here is my last try with this ContextMenu plugin (using just sample code): 这是我最后一次尝试使用ContextMenu插件 (仅使用示例代码):

<script type="text/javascript">
    $(document).live("load", function() {
        $('#testtt').contextMenu('testtt', {
            bindings: {
                'open': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Open');
                },
                'email': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Email');
                },
                'save': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Save');
                },
                'delete': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Delete');
                }
            }
        })
    });

</script>
<div id="testtt">test</div>

This is the important path that is on the page that is loaded dynamicly. 这是动态加载页面上的重要路径。

I am also using jQuery UI Sortable, but that shouldn't by problem. 我也在使用jQuery UI Sortable,但这不应该是问题。

Thanks for every helpful solution. 感谢您提供的所有有用的解决方案。

Move your $('#testtt').contextMenu... call to your Ajax success() method like below. 将您的$('#testtt')。contextMenu ...调用移至您的Ajax success()方法,如下所示。 This will ensure that your context menu is hooked up AFTER your data is loaded and displayed on the screen. 这将确保在加载数据并将其显示在屏幕上之后,您的上下文菜单已挂接。 It should be the last thing in your Ajax success(). 它应该是Ajax success()中的最后一件事。 Alternatively it could go in Ajax complete(). 另外,它也可以放在Ajax complete()中。

$.ajax({
    type: 'GET',
    url: 'PATH TO URL',
    success: function(){
        $('#testtt').contextMenu('testtt', {
            bindings: {
                'open': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Open');
                },
                'email': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Email');
                },
                'save': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Save');
                },
                'delete': function(t) {
                    alert('Trigger was '+t.id+'\nAction was Delete');
                }
            }
        });
    }
});

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

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