`

ZeroClipboard源码阅读-1.$的元素查找

 
阅读更多

ZeroClipboard源码阅读-1.$的元素查找

 

	$: function(thingy) {
		// simple DOM lookup utility function
		//获得dom对象
		if (typeof(thingy) == 'string') thingy = document.getElementById(thingy);
		if (!thingy.addClass) {
			// extend element with a few useful methods
			thingy.hide = function() { this.style.display = 'none'; };
			thingy.show = function() { this.style.display = ''; };
			thingy.addClass = function(name) 
                       { this.removeClass(name); this.className += ' ' + name; };
			thingy.removeClass = function(name) {
				//先在数组中获得name所在的下标,若存在该元素则通过splice在数组中删除
				var classes = this.className.split(/\s+/);
				var idx = -1;
				for (var k = 0; k < classes.length; k++) {
					if (classes[k] == name) { idx = k; k = classes.length; }
				}
				if (idx > -1) {
					classes.splice( idx, 1 );
					this.className = classes.join(' ');
				}
				return this;
			};
			thingy.hasClass = function(name) {
				//反正 !!(0,null,"",undefinied)为空
				return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
			};
		}
		return thingy;
	},
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics