IE 问题:使用 javascript 将表单提交到 iframe [英] IE Issue: Submitting form to an iframe using javascript

查看:23
本文介绍了IE 问题:使用 javascript 将表单提交到 iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆