当我尝试使用 iframe 标签添加事件滚动时出现问题。通常,我使用带有 div 标签的滚动事件,效果很好。但是当我在 iframe 标签中添加滚动事件来检测用户滚动 pdf 页面时,它不起作用。为什么我不能访问 iframe 中的 html 元素?,我在下面检查了代码:
我尝试使用 iframe 添加 javascript 滚动事件:
HTML 代码:
<iframe id="myframe" src="doc.pdf"></iframe>
JavaScript 代码:
document.getElementById('myframe').onscroll = function(){
alert('scrolling page');
};
原文由 DevScripts 发布,翻译遵循 CC BY-SA 4.0 许可协议
An
iframe
does not have a scroll method, thedocument
of theiframe
does - you need to reference the innerdocument
rather than your<iframe>
标签。您可以使用
iframe.contentDocument
引用它:请参阅: https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement 了解更多信息