首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery阻止默认设置似乎被卡住了,需要额外的单击。

问jquery阻止默认设置似乎被卡住了,需要额外的单击。
EN

Stack Overflow用户
提问于 2011-12-04 00:06:10
回答 1查看 678关注 0票数 0

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

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

这并不会导致浏览器跟随链接,但是在对话框关闭后用我的鼠标进行第二次单击就可以了。

这个插件的使用方式类似于这个$('a').submitConfirm();

这是插件

代码语言:javascript
复制
;(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 );
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-04 00:43:08

您必须传递一个函数,其中包含您想要做的插件。

当您在javascript底部设置插件的默认参数时,请添加这一行。

代码语言: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')。

代码语言:javascript
复制
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 ^
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8372110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档