javascript - this.class 与 this.getAttribute ('class' )

标签 javascript html

我有一个单选按钮,当它被按下时,一个函数被调用,它接受按钮的类作为参数。所以像这样:

<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,但我一直得到 undefinedthis.class 不是一件可以接受的事情吗?我在代码的其他地方使用了 this.id,效果很好,但出于某种原因,当我想引用类时,我需要使用 getAttribute

所以本质上我想问的是 this.classthis.getAttribute('class') 之间有什么区别?

最佳答案

使用 className 获取类属性的当前值。我认为这背后的原因是因为类是一个保留字。

关于javascript - this.class 与 this.getAttribute ('class' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25366726/

相关文章:

javascript - Angularjs如何在$scope中包含IF CONTAINS?

javascript - 必须在 Backbone.js 中指定 'url' 属性或函数错误

django - 花括号和百分号在html中是什么意思?

javascript - 在滚动条上绘制/显示路径

javascript - 使用nodeJs无页面刷新的网站

javascript - jQuery - 灯箱停留和页面滚动

javascript - 为什么我的 Javascript for 循环是连接数字而不是相加?

javascript - 如何在 React 中动态分配属性?

javascript - React TypeScript 2.3 -> 类型安全 React Bootstrap FormControl onChange

html - 具有初始和 url() 的多个背景图像