javascript - 使用 iframe.style.width 设置 iframe 宽度不起作用

标签 javascript

我在 iframe 中动态打开一个新文件,并尝试(没有成功)将打开的 iframe 设置为固定宽度:

var iframe = document.createElement('iframe');

// tried this guy
iframe.style.width = 970 + 'px';

// tried this guy
iframe.width = 970 + 'px';

// tried this guy
iframe.setAttribute('width', '970px');


window.open(url, iframe);

但没有成功,iframe 总是以 1200 宽度打开

最佳答案

IFRAME 是页面“inline-frame”的一部分,您无法通过“window.open”打开它。您可以将其附加到当前文档中,例如

var iframe = document.createElement('iframe');

iframe.style.width = '970px';
iframe.src = url;

document.body.appendChild(iframe);

如果您确实想打开一个新窗口 - 只需将窗口规范作为 window.open 的第三个参数传递即可:

window.open(url, "mywindow", "width=970");

关于javascript - 使用 iframe.style.width 设置 iframe 宽度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922987/

相关文章:

javascript - 两侧的 CSS 垂直菜单

javascript - 为什么react js中类函数方法不被调用?

javascript - 如果没有箭头功能,DOM 事件将无法工作

javascript - 在 Jquery 中输入文本

javascript - 如何找到可调整大小的文本区域的自动换行点

javascript - ES6 继承 : uses `super` to access the properties of the parent class

javascript - javascript中的静态方法可以调用非静态方法吗

javascript - 为什么我的按钮没有使用 ng-disabled 禁用?

javascript - jQuery 无法与 cakephp 2.0 一起使用

javascript - NodeJS SetImmediate 和回调函数