因此,我试图使用jQuery/javascript在iframe srcdoc中获取一个元素,并附加一个单击处理程序。例如..。
<iframe id="iframe" srcdoc="<html><head></head><body><input type="button" id="button" /></body></html>"></iframe>
下列代码不起作用:
$("#iframe").find("#button").on("click", function{ alert('clicked'); });
也不使用$("#iframe").contents()或其他几种方法。
如何将事件处理程序附加到Iframe中的元素?
发布于 2015-08-30 12:02:53
要处理的JS单击:
$("#iframe").contents().find("body").find("#button").click(function() {alert('clicked')});
HTML更改:
<iframe id="iframe" srcdoc="<html><head></head><body><input type='button' id='button' /></body></html>"></iframe>
编辑:使用JS加载iframe:
$("#container").append($("<iframe />").attr({"srcdoc":"<html><head></head><body><input type='button' id='button' /></body></html>", "id":"iframe" }));
$("#iframe").load( function() {
$("#iframe").contents().find("body").find("#button").click(
function () {alert('clicked')}
);
});
工作固定连接:http://jsfiddle.net/hufrmcLt/
编辑:http://jsfiddle.net/hufrmcLt/1/
https://stackoverflow.com/questions/32296238
复制相似问题