首页 > 代码库 > js模拟jq获取id

js模拟jq获取id

js模拟jq获取id:

(jquery)需要自己添加

<!DOCTYPE html><html lang="en"><head>	<meta charset="UTF-8">	<title>js模拟jq的点击效果</title>	<style>		* {margin: 0; padding: 0;}		#btn {display:block;height: 30px; width: 100px;margin: 100px auto 0;}	</style></head><body>	<button id="btn">点击</button>	<script>		(function(){			function $(targetID) {				var word = targetID.substring(0,1);				if (word == ‘#‘) {					var ID = targetID.replace(/#/g,‘‘);					var obj = document.getElementById(ID);					obj.click = function(fn) {						this.onclick = fn;					}					return obj;				}			}			window.jQuery = $;			window.$ = window.jQuery;		})();		$(‘#btn‘).click(function() {			alert(‘qqq‘);		});	</script></body></html>

  

js模拟jq获取id