dom中classList对象常用方法
添加新的类名,如已经存在,取消添加
classList.add( newClassName );
var str = document.getElementById('str'); str.classList.add('newClassname')确定元素中是否包含指定的类名,返回值为true 、false;
classList.contains( oldClassName );
var str = document.getElementById('str'); str.classList.contains('newClassname') //return true/false移除已经存在的类名;
classList.remove( oldClassName );
var str = document.getElementById('str'); str.classList.add('newClassname') //移除类名newClassname如果classList中存在给定的值,删除它,否则,添加它;(适用于开关切换)
classList.toggle( className );
var str = document.getElementById('str'); str.classList.add('select')//切换classstr.classList.add('select',i<10)//第二参数是条件类名替换
classList.replace( oldClassName,newClassName );
var str = document.getElementById('str'); str.classList.add('select','newSelect')
浙公网安备 33010602011771号