javascript document.getElementById("xxx").name = "sample" 在 Firefox 中不起作用,但在 IE 中有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5104109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:57:13  来源:igfitidea点击:

document.getElementById("xxx").name = "sample" not working in firefox but works in IE

javascriptinternet-explorermozilla

提问by sushil bharwani

Can somebody help on this one. I am trying to set the name of a element dynamically.

有人可以帮忙解决这个问题。我正在尝试动态设置元素的名称。

采纳答案by Jo?o Neves

Try using setAttribute.

尝试使用setAttribute

Example:

例子:

var element = document.getElementById("xxx");
element.setAttribute("name", "sample");

回答by Gabriele Petrioli

Works just fine at http://jsfiddle.net/gaby/5ve4g/

http://jsfiddle.net/gaby/5ve4g/ 上工作得很好

to check run the following script

检查运行以下脚本

document.getElementById('xxx').name = 'some-name';
var el = document.getElementById('xxx');
alert(el.name);

If you are using firebug to check it, then know that if the element does not officially has a nameattribute then it will not show in the DOM.

如果您使用 firebug 来检查它,那么知道如果该元素没有正式的name属性,那么它将不会显示在 DOM 中。

But the element does have that property now..

但是该元素现在确实具有该属性..