我想使用获取ID为参数的处理程序来处理click事件。iframeiframe我可以onClick通过JavaScript 添加事件处理程序,如下所示,它可以正常运行:iframe.document.addEventListener('click', clic, false);但是在这种情况下,我无法将参数传递给clic()。我尝试打印this.id,clic()但没有结果。onClick HTML属性根本不起作用,不调用处理程序。<html><head><script type="text/javascript">function def() { myFrame.document.designMode = 'on';}function clic(id) { alert(id);}</script></head><body onLoad="def()"><iframe id="myFrame" border="0" onClick="clic(this.id)"></iframe></body></html>
3 回答

饮歌长啸
TA贡献1951条经验 获得超3个赞
iframe没有onclick事件,但是我们可以通过使用iframe的onload事件和类似javascript的方法来实现这一点...
function iframeclick() {
document.getElementById("theiframe").contentWindow.document.body.onclick = function() {
document.getElementById("theiframe").contentWindow.location.reload();
}
}
<iframe id="theiframe" src="youriframe.html" style="width: 100px; height: 100px;" onload="iframeclick()"></iframe>
希望对您有帮助。
添加回答
举报
0/150
提交
取消