第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用event.preventDefault()后如何觸發(fā)事件

使用event.preventDefault()后如何觸發(fā)事件

慕仙森 2019-09-21 13:50:08
我想舉行一個活動,直到我準備解雇它為止$('.button').live('click', function(e){   e.preventDefault();    // do lots of stuff   e.run() //this proceeds with the normal event    }是否有與上述run()功能等效的功能?
查看完整描述

3 回答

?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

不。一旦事件被取消,便被取消。


不過,您可以稍后使用標志來確定您的自定義代碼是否已經(jīng)運行,從而重新觸發(fā)該事件-像這樣(請忽略公然的名稱空間污染):


var lots_of_stuff_already_done = false;


$('.button').on('click', function(e) {

    if (lots_of_stuff_already_done) {

        lots_of_stuff_already_done = false; // reset flag

        return; // let the event bubble away

    }


    e.preventDefault();


    // do lots of stuff


    lots_of_stuff_already_done = true; // set flag

    $(this).trigger('click');

});

更通用的變體(具有避免全局名稱空間污染的附加好處)可以是:


function onWithPrecondition(callback) {

    var isDone = false;


    return function(e) {

        if (isDone === true)

        {

            isDone = false;

            return;

        }


        e.preventDefault();


        callback.apply(this, arguments);


        isDone = true;

        $(this).trigger(e.type);

    }

}

用法:


var someThingsThatNeedToBeDoneFirst = function() { /* ... */ } // do whatever you need

$('.button').on('click', onWithPrecondition(someThingsThatNeedToBeDoneFirst));

額外的超簡約jQuery插件,Promise支持:


(function( $ ) {

    $.fn.onButFirst = function(eventName,         /* the name of the event to bind to, e.g. 'click' */

                               workToBeDoneFirst, /* callback that must complete before the event is re-fired */

                               workDoneCallback   /* optional callback to execute before the event is left to bubble away */) {

        var isDone = false;


        this.on(eventName, function(e) {

            if (isDone === true) {

                isDone = false;

                workDoneCallback && workDoneCallback.apply(this, arguments);

                return;

            }


            e.preventDefault();


            // capture target to re-fire event at

            var $target = $(this);


            // set up callback for when workToBeDoneFirst has completed

            var successfullyCompleted = function() {

                isDone = true;

                $target.trigger(e.type);

            };


            // execute workToBeDoneFirst callback

            var workResult = workToBeDoneFirst.apply(this, arguments);


            // check if workToBeDoneFirst returned a promise

            if (workResult && $.isFunction(workResult.then))

            {

                workResult.then(successfullyCompleted);

            }

            else

            {

                successfullyCompleted();

            }

        });


        return this;

    };

}(jQuery));

用法:


$('.button').onButFirst('click',

    function(){

        console.log('doing lots of work!');

    },

    function(){

        console.log('done lots of work!');

    });


查看完整回答
反對 回復 2019-09-21
?
一只斗牛犬

TA貢獻1784條經(jīng)驗 獲得超2個贊

isDefaultPrevented像這樣覆蓋屬性:


$('a').click(function(evt){

  evt.preventDefault();


  // in async handler (ajax/timer) do these actions:

  setTimeout(function(){

    // override prevented flag to prevent jquery from discarding event

    evt.isDefaultPrevented = function(){ return false; }

    // retrigger with the exactly same event data

    $(this).trigger(evt);

  }, 1000);

}

恕我直言,這是使用完全相同的數(shù)據(jù)重新觸發(fā)事件的最完整方法。


查看完整回答
反對 回復 2019-09-21
  • 3 回答
  • 0 關注
  • 1207 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號