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

  

原文地址:https://www.cnblogs.com/handsomehan/p/5893848.html