直接上原始的代码
<html>
<head>
<title> JS中getAttribute方法 </title>
</head>
<body>
<input type="text" class = "textclass" name ="inputtext" value="123"/>
</body>
<script type="text/javascript">
var i = document.getElementsByName("inputtext")[0];
alert(i.getAttribute("class"));
</script>
</html>
无法获取到input标签中class属性值
问题原因是EditPlus默认是使用IE,所以要获取class属性,可用classname属性来替代。element.getAttribute(“classname”)来获取。
<html>
<head>
<title> JS中getAttribute方法 </title>
</head>
<body>
<input type="text" class = "textclass" name ="inputtext" value="123"/>
</body>
<script type="text/javascript">
var i = document.getElementsByName("inputtext")[0];
alert(i.getAttribute("classname"));
</script>
</html>
可以获取到class属性
本文介绍在JavaScript中使用getAttribute方法获取HTML元素class属性时遇到的问题及解决办法。由于浏览器兼容性问题,在使用IE作为默认浏览器的编辑器(如EditPlus)中,getAttribute方法无法正确获取class属性,可通过使用getAttribute('classname')解决。

被折叠的 条评论
为什么被折叠?



