jquery Prevent default 似乎卡住了,需要额外的单击

发布于 2024-12-19 07:33:19 字数 1908 浏览 1 评论 0原文

我正在编写一个 jquery 插件,用于在单击链接时显示 jquery ui 对话框,以在链接之前提供确认对话框。

我遇到的问题是,当使用“是”按钮关闭对话框时,插件使用 $(element).trigger( 'click' ); 来触发原始锚点上的单击事件元素。

这不会导致浏览器跟踪该链接,但是在对话框关闭后再次单击鼠标确实可以工作。

插件的使用方式如下 $('a').submitConfirm();

这是插件

;(function ( $, window, document, undefined )
{
    var pluginName = "submitConfirm";

    var Plugin = function( element )
    {
        var confirmed = false;

        var dialog = $( '<div style="display:none;">' )
        .html( 'Visit this link?' )
        .dialog(
        {
            modal: true,
            title: 'Visit Link?',
            autoOpen: false,
            buttons :
            [
               {
                    text: 'Yes',
                    click: function( event )
                    {
                        confirmed = true;
                        dialog.dialog( "close" );
                        $(element).trigger( 'click' );
                    }
                },
                {
                    text: 'No',
                    click: function( event )
                    {
                        confirmed = false;
                        dialog.dialog( "close" );
                    }
                }
            ]
        });

        $(element).bind( 'click',
        function( event )
        {
            if ( ! confirmed )
            {
                dialog.dialog( "open" );
                event.preventDefault();
            }
        });
    };

    // Init the plugin
    $.fn[pluginName] = function( options )
    {
        return this.each(function ()
        {
            // Prevent re-instantiation
            if ( !$.data(this, 'plugin_' + pluginName) )
            {
                $.data(this, 'plugin_' + pluginName,
                new Plugin( this, options ));
            }
        });
    };
})( jQuery );

I'm writing a jquery plugin to display a jquery ui dialog when links are clicked to provide a confirmation dialog before the link is followed.

The problem i'm having is that when closing the dialog using the "Yes" button, the plugin uses $(element).trigger( 'click' ); to fire the click event on the original anchor element.

This does not cause the browser to follow the link, however a second click with my mouse after the dialog closes does work.

The plugin is used like this $('a').submitConfirm();

Here is the plugin

;(function ( $, window, document, undefined )
{
    var pluginName = "submitConfirm";

    var Plugin = function( element )
    {
        var confirmed = false;

        var dialog = $( '<div style="display:none;">' )
        .html( 'Visit this link?' )
        .dialog(
        {
            modal: true,
            title: 'Visit Link?',
            autoOpen: false,
            buttons :
            [
               {
                    text: 'Yes',
                    click: function( event )
                    {
                        confirmed = true;
                        dialog.dialog( "close" );
                        $(element).trigger( 'click' );
                    }
                },
                {
                    text: 'No',
                    click: function( event )
                    {
                        confirmed = false;
                        dialog.dialog( "close" );
                    }
                }
            ]
        });

        $(element).bind( 'click',
        function( event )
        {
            if ( ! confirmed )
            {
                dialog.dialog( "open" );
                event.preventDefault();
            }
        });
    };

    // Init the plugin
    $.fn[pluginName] = function( options )
    {
        return this.each(function ()
        {
            // Prevent re-instantiation
            if ( !$.data(this, 'plugin_' + pluginName) )
            {
                $.data(this, 'plugin_' + pluginName,
                new Plugin( this, options ));
            }
        });
    };
})( jQuery );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年慕年 2024-12-26 07:33:19

您必须将包含您想要执行的操作的函数传递给插件。
当您在 javascript 底部设置插件的默认参数时添加此行。

$(function()
{
    $('a').submitConfirm(
    {
        html: 'Are you sure?',
        onConfirm: function(event){ // Do what you want in this function.
            alert('Confirmed.. Now what?.. Redirect?.. ?? ');
            // window.location = $(this).attr('href'); // redirect
                },
        beforeShow: function( dialog )
        {
            dialog.html( 'visit google?' );
        }
    });
});

更新
看看这个 JSfiddle --> http://jsfiddle.net/kmTtQ/6/
我更改了下面的行。基本上,我们想要向元素添加一个 .click 事件,然后添加 .trigger('click') 该单击事件。

if ( confirmed ){
    console.log( element, elementEvent, event.isDefaultPrevented );
    // .on() = jQuery 1.7+, for < 1.7 use .bind or .live. Aliases of .on() as of 1.7
    $(element).on('click', function(){ // bind our element with the click
        event.view.window.location = element.href; // on click redirect
    });

    $(element).trigger( 'click' ); // We want to trigger the ^ click event ^
}

You have to pass a function containing what you want to do to the plugin.
Add this line when you are setting the default parameters for the plugin at the bottom of your javascript.

$(function()
{
    $('a').submitConfirm(
    {
        html: 'Are you sure?',
        onConfirm: function(event){ // Do what you want in this function.
            alert('Confirmed.. Now what?.. Redirect?.. ?? ');
            // window.location = $(this).attr('href'); // redirect
                },
        beforeShow: function( dialog )
        {
            dialog.html( 'visit google?' );
        }
    });
});

Update
Check out this JSfiddle --> http://jsfiddle.net/kmTtQ/6/
I changed the lines below. Basically, we want to add a .click event to the element, then .trigger('click') that click.

if ( confirmed ){
    console.log( element, elementEvent, event.isDefaultPrevented );
    // .on() = jQuery 1.7+, for < 1.7 use .bind or .live. Aliases of .on() as of 1.7
    $(element).on('click', function(){ // bind our element with the click
        event.view.window.location = element.href; // on click redirect
    });

    $(element).trigger( 'click' ); // We want to trigger the ^ click event ^
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文