使用 jQuery 切换多个 类
Toggle Through Multiple Classes with jQuery
我试图在用户单击按钮时循环 3 classes。我找到了 same challenge as me 的人,但我缺乏对 post 发表任何后续评论的声誉。
这是循环遍历 classes 的原始代码:
$('.one, .two, .three').click(function() {
this.className = {
three : 'one', one: 'two', two: 'three'
}[this.className];
});
我修改如下:
$('#next').click(function() {
this.className = {
liscombe : 'digby', digby: 'keltic', keltic: 'liscombe'
}[this.className];
});
这按预期工作并循环遍历 #next 元素上的 classes,但是我希望在单击 #next 时在 body 标签上发生 class 更改。我不确定如何修改 'this.' 我试过 body.className 但那没有用。
如何修改才能让正文 class 在点击时发生变化?
document.body.className
应该有效
'this' 只是传递您在初始选择中使用的任何选择器。所以它通过了#next。将 "this" 更改为 $('body')
我试图在用户单击按钮时循环 3 classes。我找到了 same challenge as me 的人,但我缺乏对 post 发表任何后续评论的声誉。
这是循环遍历 classes 的原始代码:
$('.one, .two, .three').click(function() {
this.className = {
three : 'one', one: 'two', two: 'three'
}[this.className];
});
我修改如下:
$('#next').click(function() {
this.className = {
liscombe : 'digby', digby: 'keltic', keltic: 'liscombe'
}[this.className];
});
这按预期工作并循环遍历 #next 元素上的 classes,但是我希望在单击 #next 时在 body 标签上发生 class 更改。我不确定如何修改 'this.' 我试过 body.className 但那没有用。
如何修改才能让正文 class 在点击时发生变化?
document.body.className
应该有效
'this' 只是传递您在初始选择中使用的任何选择器。所以它通过了#next。将 "this" 更改为 $('body')