Iframe的高度改变,如果点击

iframe height change if clicked

本文关键字:如果 改变 高度 Iframe      更新时间:2023-09-26

我需要在点击时改变iframe的高度。iframe是用下面的代码创建的:

var box = document.createElement('iframe');
box.id = "statasa";
box.setAttribute('onclick', 'getmap()');
box.src = 'http://www.mysite.com/map.php';
box.frameBorder = 1;
box.style.border = 1;
box.style.overflow = 'hidden';
box.style.cursor = 'pointer';
box.style.width = '300px';
box.style.height =  '720px';
box.style.position = 'absolute';
document.getElementsByTagName('body')[0].appendChild(box);

函数是:

function getmap() {
    d = document.getElementById('statasa');
    d.style.width="150px";
    d.style.height="150px";
}

错误在哪里?有没有更简单的方法?

你无法检测iframe中的click事件。参见使用JavaScript检测点击进入Iframe


对于问题的第二部分,一种简化的方法是:

box.setAttribute('onclick', 'getmap()');

使用:

box.onclick = getmap();