我可以使用JavaScript来设置'name'属性吗? [英] Can I use JavaScript to set the 'name' attribute?

查看:82
本文介绍了我可以使用JavaScript来设置'name'属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 SitePoint (和我自己的实验),IE实现setAttribute ()是错误的和不可靠的。

According to SitePoint (and my own experiments), the IE implementation of "setAttribute()" is buggy and unreliable.

另外根据 SitePoint 名称属性是只读的。

Also according to SitePoint, the name attribute is read-only.

有另一种方式吗可以在元素上设置名称属性?我需要使用单选按钮。如果可能,我想要一个没有jQuery的解决方案,因为我目前没有使用该库。

Is there another way I can set the name attributes on elements? I need this for use with radio buttons. If possible, I'd like a solution without jQuery, as I'm currently not using the library.

提前感谢!

推荐答案

Sitepoint 谎言正在谈论name的另一种用法(见Anthony的评论)。它不是只读的,它只是有一个长期的IE错误(直到v7),其中在表单字段上设置名称只是部分有效。单选按钮特别不能正确接受。

Sitepoint liesis talking about a different usage of ‘name’ (see Anthony's comment). It's not read-only, it's just there's a long-standing IE bug (up to v7) where setting ‘name’ on form fields is only partially effective. Radio buttons in particular don't accept it properly.

Microsoft认可的解决方案这里详细是使用IE的版本的createElement调用来设置属性的可怕的错误同时:

The Microsoft-endorsed solution, as detailed here is to use a horrific misfeature of IE's version of the createElement call to set attributes at the same time:

var radio= document.createElement('<input type="radio" name="test" value="a" />');

可能更好的方法就是使用好的old innerHTML,例如:

Probably a better way would simply be to use good old innerHTML, eg.:

var div= document.createElement('div');
div.innerHTML= '<input type="radio" name="test" value="a" />';
var radio= div.firstChild;

这篇关于我可以使用JavaScript来设置'name'属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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