我正在寻找动态设置在我的应用程序中动态创建的HTML Input元素的ID属性。在IE中动态设置输入元素的id属性:替代setAttribute方法
我的实现可以在Firefox中使用setAttribute方法。任何想法或解决方案在IE中的工作实现将不胜感激。
var hiddenInput = document.createElement("input");
hiddenInput.setAttribute("id", "uniqueIdentifier");
hiddenInput.setAttribute("type", "hidden");
hiddenInput.setAttribute("value", ID);
hiddenInput.setAttribute("class", "ListItem");
我修改了一些与这个问题有关的博客示例代码,提示以下workround。再次,Firefox位运行良好,但IE位不确定
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");
}
干杯。
你测试哪个版本的IE? – gor 2011-01-31 15:14:42
Internet Explorer 8 – Terman 2011-01-31 15:35:18
什么不工作?是否有错误讯息?出了什么问题? – 2011-01-31 15:51:28