我有一个单选按钮,当它被按下时,一个函数被调用,它接受按钮的类作为参数。所以像这样:
<input class="myclass" type="radio" name="name" value="x" checked="true" onClick="myFunction(this.value, this.getAttribute('class'))"></input>
请注意,在我的 onClick 中,我有 this.getAttribute('class')
而不是简单的 this.class
。我尝试使用 this.class,但我一直得到 undefined
。 this.class
不是一件可以接受的事情吗?我在代码的其他地方使用了 this.id
,效果很好,但出于某种原因,当我想引用类时,我需要使用 getAttribute
。
所以本质上我想问的是 this.class
和 this.getAttribute('class')
之间有什么区别?
最佳答案
使用 className 获取类属性的当前值。我认为这背后的原因是因为类是一个保留字。
关于javascript - this.class 与 this.getAttribute ('class' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25366726/