2014-09-18 31 views
-1

我正在嘗試使pjax:complete事件處理程序正常工作。下面我勾畫了這種情況:addEventListener在事件名稱中不能與冒號(:)一起使用

我正在研究GitHub的Chrome擴展,它不包含jQuery,我不想包含它。我確實想聽pjax事件,並在發生時做一些事情。然而,下面的例子似乎只有在使用jQuery的$.on函數時才起作用。我也爲此創建了一個簡單的JSFiddle,但沒有使用真正的pjax事件,而是模擬它。 You can find that here.如果你運行它,你會看到jQuery eventhandler被調用2次,但是VanillaJS只有一次。

$(document).on('pjax:complete', function() { 
    console.log('jQuery pjax event is called'); 
}); 

document.addEventListener('pjax:complete', function() { 
    console.log('VanillaJS pjax event is called'); 
}); 

document.dispatchEvent((new Event('pjax:complete'))); // This works 
$(document).trigger('pjax:complete'); // this doesn't (FYI: actual function used can be found [here][1]) 

想要的結果是,即使從jQuery調用VanillaJS事件,也會調用它。

1:https://github.com/defunkt/jquery-pjax/blob/master/jquery.pjax.js#L181

+0

您可以用[最小的,完整的驗證示例]重現此問題(http://stackoverflow.com/help/mcve/);在[JS小提琴](http://jsfiddle.net/),或與'[Stack Snippet](http://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html代碼片段/?cb = 1)'在你的問題? – 2014-09-18 19:09:36

+0

1)您在示例中使用的是'complete'和'completed'不一致。 2)冒號在行爲上沒有任何區別 - 這就是使用任何事件名稱時會發生的情況。 – apsillers 2014-09-18 19:11:15

+0

@apsillers我不這麼認爲,我修復了不同的引用 – jeroenvisser101 2014-09-18 19:37:52

回答

相關問題