触发选择器上的单击事件

时间:2014-04-15 13:57:36

标签: javascript jquery click selector

我试图获取点击事件以取消使用选择器的插件的手动操作。

插件设置。

 $( function ()
            {
                $.contextMenu( {
                    selector: '.context-menu-one',
                    callback: function ( key, options )
                    {
                        var m = "clicked: " + key;
                        window.console && console.log( m ) || alert( m );
                    },
                    items: {
                        "edit": { name: "Edit", icon: "edit" },
                        "cut": { name: "Cut", icon: "cut" },
                        "copy": { name: "Copy", icon: "copy" },
                        "paste": { name: "Paste", icon: "paste" },
                        "delete": { name: "Delete", icon: "delete" },
                        "sep1": "---------",
                        "quit": { name: "Quit", icon: "quit" }
                    }
                } );

                $( '.context-menu-one' ).on( 'click', function ( e )
                {
                    alert( 'clicked', this );
                } )
            } );

我尝试了两种解雇方法,但我无法使其发挥作用。 $(' .context-menu-one')。strigger('点击'); 要么 $(' .context-menu-one')。click();

是否有一种特殊的方法可以在选择器上触发。我认为这是在某处解释的,但我的搜索已经到了终点。

---------更新添加更多周围的代码-----------

 //local functions
    $( function ()
    {//override right click.

        $( this ).bind( "contextmenu", function ( e )
        {
            alert( 1 );// users right clicked
            var overRide = true;

            //set up the right click menu
            $( function ()
            {
                $.contextMenu( {
                    selector: '.context-menu-one',
                    callback: function ( key, options )
                    {
                        var m = "clicked: " + key;
                        window.console && console.log( m ) || alert( m );
                    },
                    items: {
                        "edit": { name: "Edit", icon: "edit" },
                        "cut": { name: "Cut", icon: "cut" },
                        "copy": { name: "Copy", icon: "copy" },
                        "paste": { name: "Paste", icon: "paste" },
                        "delete": { name: "Delete", icon: "delete" },
                        "sep1": "---------",
                        "quit": { name: "Quit", icon: "quit" }
                    }
                } );

                $( '.context-menu-one' ).on( 'click', function ( e )
                {
                    alert( 'clicked', this );
                } )
            } );

            //bring up the menu
            $( '.context-menu-one' ).trigger( {
                type: 'mousedown',
                which: 1
            } );

            if (overRide)  e.preventDefault();
        } );
    } );

我的目的只是触发右键单击,以便手动启动菜单而不是基于事件。

2 个答案:

答案 0 :(得分:0)

基于文档demo的文档。下面的示例等待加载文档,并在延迟1秒后以编程方式打开上下文菜单:

<body>
    <div class="dummyDiv box menu-1"/>
</body>
<script>
$(function(){
    $.contextMenu({
        selector: '.dummyDiv', 
        trigger: 'none',
        items: {
            "edit": {name: "Edit", icon: "edit"},
            "cut": {name: "Cut", icon: "cut"},
            "copy": {name: "Copy", icon: "copy"},
            "paste": {name: "Paste", icon: "paste"},
            "delete": {name: "Delete", icon: "delete"},
            "sep1": "---------",
            "quit": {name: "Quit", icon: "quit"}
        }
    });

    setTimeout(function(){
        $('.dummyDiv').contextMenu();
    }, 1000);       
 });
</script>

答案 1 :(得分:0)

请尝试triggering

$('.context-menu-one').trigger({
    type: 'mousedown',
    which: 1
});