我试图使用 javascript 创建一个 iframe 元素,如下所示:
I was trying to create an iframe element using javascript, like so:
var iframe = document.createElement('iframe'); iframe.setAttribute('name', 'frame_x');但是,当我尝试使用新创建的 iframe 作为目标提交表单时,IE 会打开一个新窗口,而不是使用 iframe.
However, when I tried submitting a form using the newly-created iframe as target, IE opens up a new window, instead of using the iframe.
form.setAttribute('target', 'frame_x'); form.submit();这在 Firefox 中完美运行.此外,iframe 已创建,但未使用.
This works perfectly in Firefox. Also, the iframe gets created, but is not used.
推荐答案您遇到了 Internet Explorer 中的错误.
You've hit a bug in Internet Explorer.
您不能在IE中使用标准DOM方法.setAttribute('name',值);
You CAN NOT set the name attribute of ANY element in IE using the standard DOM Method .setAttribute('name', value);
在 IE 中(在 IE8 标准模式下运行的版本 8 之前)此方法将无法设置名称属性.
In IE (before version 8 running in IE8 standards mode) this method will not work to set the name attribute.
您需要使用以下其中一项:
You need to use one of the following:
//A (any browser) var iframe = document.createElement('iframe'); iframe.name = 'frame_x'; //B (only in IE) var iframe = document.createElement('<iframe name="frame_x"/>'); //C (use a JS library that fixes the bug (e.g. jQuery)) var iframe = $('<iframe name="frame_x"></iframe>'); //D (using jQuery to set the attribute on an existing element) $('iframe:first').attr('name','frame_x');更多推荐
IE 问题:使用 javascript 将表单提交到 iframe
发布评论