我发现getElementsByName有些控件不支持,如
,这是为什么?

oceanmeng 2015-08-20 07:20:59
我发现getElementsByName有些控件不支持,如<br><img> ,这是为什么?
document.getElementsByName('setBr').length
document.getElementsByName('setpImg' ).length


<br id='setpBr1' name='setBr'>
<br id='setpBr2' name='setBr'>
<br id='setpBr3' name='setBr'>
<br id='setpBr4' name='setBr'>
<br id='setpBr5' name='setBr'>


<img id='setpImg1' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg2' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg3' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg4' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg5' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
...全文
146 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝶恋花雨 2015-08-20
  • 打赏
  • 举报
回复
引用 4 楼 oceanmeng 的回复:
在IE8 中 'setBr' 返回0 啊,不是5
参考 http://bbs.csdn.net/topics/340204032
天际的海浪 2015-08-20
  • 打赏
  • 举报
回复
按标准,br元素是没有name属性的,它的name是自定义的属性,用getElementsByName方法是不能获取到的。 但是有一些浏览器对于元素自定义的name属性,用getElementsByName方法也能获取到。
oceanmeng 2015-08-20
  • 打赏
  • 举报
回复
在IE8 中 'setBr' 返回0 啊,不是5
蝶恋花雨 2015-08-20
  • 打赏
  • 举报
回复
上面的回复错了
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
	<script>
		function test() {
		alert(document.getElementsByName("setpImg").length);//reslut:5
			var hh = document.getElementsByName("setpImg");
			alert(hh.length);																		//reslut:5
			var hhh = document.getElementsByName("setBr");
			alert(hhh.length);																	//reslut:5
		}
	</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    													<br id='setpBr1' name='setBr'>
<br id='setpBr2' name='setBr'>
<br id='setpBr3' name='setBr'>
<br id='setpBr4' name='setBr'>
<br id='setpBr5' name='setBr'>
 
 																												 
<img id='setpImg1' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg2' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg3' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg4' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
<img id='setpImg5' name='setpImg' src='./img/going_down.png' height='59' width='36'/>
					<input id="btntets" onclick="test();" type="button" value="testt"/>
    </div>
    </form>
</body>
</html>
蝶恋花雨 2015-08-20
  • 打赏
  • 举报
回复
var obj = document.getElementsByTagName("img"); obj[i].height
蝶恋花雨 2015-08-20
  • 打赏
  • 举报
回复
支持这些,不支持的是 div,span等

87,955

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧