在IE中动态设置输入元素的id属性:setAttribute方法的替代方法 [英] setting the id attribute of an input element dynamically in IE: alternative for setAttribute method

查看:928
本文介绍了在IE中动态设置输入元素的id属性:setAttribute方法的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看动态设置在我的应用程序中动态创建的HTML Input元素的ID属性。

I'm looking at dynamically setting the ID attribute of HTML Input elements which are created dynamically in my application.

我的实现在Firefox的setAttribute方法中正常工作。对IE中的工作实现的任何想法或解决方案将不胜感激。

My implementation works fine with the setAttribute method in Firefox. Any ideas or solutions on a working implementation in IE would be appreciated.

 var hiddenInput = document.createElement("input");
    hiddenInput.setAttribute("id", "uniqueIdentifier");
    hiddenInput.setAttribute("type", "hidden");                     
    hiddenInput.setAttribute("value", ID);
    hiddenInput.setAttribute("class", "ListItem");

我从与此问题相关的博客中修改了一些示例代码,提示以下工作环境。再次,Firefox位工作得很好,但IE位不执行

I modified some sample code from blogs relating to this problem that suggest the following workround. Again the Firefox bit works well but the IE bit doens't

var hiddenInput = null;

try { 
hiddenInput  = document.createElement('<input name=\''+"hiddenInputName"+'\'   />');
                    hiddenInput.id = "uniqueIdentifier";
                    //alert(document.getElementById("uniqueIdentifier")); 
                   hiddenInput.type = "hidden";
                } catch (e) { }            
                if (!hiddenInput || !hiddenInput.name) { // Not in IE, then
                     var hiddenInput = document.createElement("input");
    hiddenInput.setAttribute("id", "uniqueIdentifier");
    hiddenInput.setAttribute("type", "hidden");                     

            }

干杯。

推荐答案

代码在IE7和Chrome中工作:

This code work in IE7 and Chrome:

var hiddenInput = document.createElement("input");
    hiddenInput.setAttribute("id", "uniqueIdentifier");
    hiddenInput.setAttribute("type", "hidden");                     
    hiddenInput.setAttribute("value", 'ID');
    hiddenInput.setAttribute("class", "ListItem");

$('body').append(hiddenInput);

可能还有其他问题?

这篇关于在IE中动态设置输入元素的id属性:setAttribute方法的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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