<div id="tests" class="a b c" style="color:#f00">123</div>
var test = tests.firstChild.nodeValue; tests = tests.childNodes[0]; tests.insertData(0,789);//从前插入789 var t2 = document.querySelector("#tests");//获取id号 console.log(t2.mozMatchesSelector(".c"));////t2是否含有c的classname
解决各浏览器兼容性
function hasClass(ele,txt){ var ary = ["matchesSelector","mozMatchesSelector","msMatchesSelector","webkitMatchesSelector"], i = ary.length-1, t = false; for(;i>=0;i--){ if(typeof ele[ary[i]] == "function"){ if(ele[ary[i]].call(ele,txt)){ t = true; } } } return t; } console.log(hasClass(t2,".a"));