现代浏览器原生js获取id号方法

<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"));

 

posted @ 2015-09-13 11:31  唸随爱  阅读(1915)  评论(0编辑  收藏  举报