![](/img/trans.png)
[英]How to trigger onchange event in an IFrame mithril element when a codemirror editor in the srcdoc of the IFrame changes?
[英]iframe scroll event not fired when using srcdoc=
我正在使用iframe显示html文档。 我需要在iframe中检测滚动事件。 使用src =可以正常工作,但是srcdoc =不会触发滚动事件。
HTML:
<iframe id="TermsConditionsText" srcdoc="<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /><title></title></head><body><div class="Section0"> ... </div></body></html>" class="form-control" style="overflow-y: scroll; height: 600px;"></iframe>
jQuery的:
jQuery(document).ready(function () {
var termsWereScrolled = false;
$("#TermsConditionsText").scroll(function () {
if (termsWereScrolled) return true;
if ($(this).scrollTop() + $(this).innerHeight() + 2 >= $(this)[0].scrollHeight) {
termsWereScrolled = true;
}
});
});
使用srcdoc =时,iframe是否触发滚动事件?
使用srcdoc =时,iframe是否触发滚动事件?
是的,使用srcdoc
时会触发滚动事件。
iframe
是它是一个新窗口,并且具有自己的文档。 因此,您必须将事件附加到其文档而不是元素本身。 尽管如此,您要做的就是查询iframe
的内容,例如$("#TermsConditionsText").contents()
来附加事件。 看一下这个:
var iframeContents = $("#TermsConditionsText").contents();
$(iframeContents).scroll(function () {
console.log('scroll');
var html = $(this)[0].scrollingElement;
console.log('scrollHeight', html.scrollHeight);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.