是否有办法检索event
事件的DOMContentLoaded
对象,即使它在设置eventListener之前已被触发?
我找到了DOMContentLoaded
的一些时序数据,并希望也可以存储事件数据。
window.performance.timing.domContentLoadedEventStart
window.performance.timing.domContentLoadedEventEnd
我想将event
对象传递给我的回调,无论它是直接调用还是作为eventlistener的结果。
var callback = function(event){
console.log(event)
}
if (document.readyState !== "loading") {
var event = window.DOMContentLoadedEvent; // doesnt exist
callback.call(this, event);
} else {
document.addEventListener('DOMContentLoaded', callback, false)
}
我想我可以创建一个新对象并返回该对象,但我希望我的代码尽可能小。
var event = {
srcElement: document,
target:document,
timeStamp:window.performance.timing.domContentLoadedEventEnd,
type:"DOMContentLoaded",
}
callback.call(this, event);
我所做的是添加另一个事件监听器,如果被触发,肯定会在DOMContentLoaded
事件之前注册,处理程序存储event
对象;
document.addEventListener('DOMContentLoaded', function(event){
window.DOMContentLoadedEvent = event;
});
我的代码是一个非常简单的domready函数
document.addEventListener('DOMContentLoaded', function(event){
window.DOMContentLoadedEvent = event;
});
domready = function(callback) {
if (document.readyState === "loading") {
return document.addEventListener('DOMContentLoaded', callback, false);
}
return callback.call(this, window.DOMContentLoadedEvent);
}
以下代码可能驻留在DOMContentLoaded
事件触发后插入的外部脚本中。
domready(function(event){
console.log(event)
});
我是否可以在不设置其他事件监听器的情况下检索event
数据?
答案 0 :(得分:-1)
检查一下:
view
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.interactivePopGestureRecognizer?.addTarget(self, action:#selector(self.handlePopGesture))
}
func handlePopGesture(gesture: UIGestureRecognizer) -> Void {
if gesture.state == UIGestureRecognizerState.Began {
// respond to beginning of pop gesture
}
}
会显示<body>
...
<script>
var DOMevent; // global variable
document.addEventListener('DOMContentLoaded', function(event) {
DOMevent = event; // save it
}, false);
window.addEventListener('load', function () {
processEvent(DOMevent); // works fine
}, false);
function processEvent(e) {
console.log(e);
}
</script>
</body>
。
有关console.log(e);
https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
有关DOMevent
事件的更多信息
https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
更多关于'加载'事件
https://developer.mozilla.org/en-US/docs/Web/Events/load *错误示例! readyState
- &gt; DOMContentLoaded
!