![](/img/trans.png)
[英]Fire a custom event with javascript (or jquery) when my function finishes
[英]Fire JavaScript event with custom parameter and catch it with jQuery
我目前正在從頁面本身到iframe文檔觸發一個自定義JavaScript事件:
// Create iframe
var iframe = document.createElement('iframe');
iframe.setAttribute('src', 'URL to load');
document.body.appendChild(iframe);
// Event itself
var event = document.createEvent('HTMLEvents');
event.custom = 'example param';
event.initEvent('custom', false, true);
iframe.contentWindow.window.document.dispatchEvent(event);
現在,在此iframe頁面上,我可以使用jQuery。 如何捕捉事件(有效)並且也(重要)獲取自定義(對象)參數?
像這樣捕捉它:
$(document).on('custom', function(e, param) {
alert(e); // [object Object] -> normal jQuery event data without any special data
alert(e.custom); // undefined
alert(param); // undefined
});
如何在不將jQuery添加到包含iframe的頁面本身的情況下獲取自定義參數數據?
jQuery包裝了原始的DOM事件。 原始事件應該可以通過jQuery事件的originalEvent
屬性獲得。
e.originalEvent.custom
應該評估為您的自定義值,在這種情況下為'example param'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.