- <script type="text/javascript">
- var obj = {};
- window.onload = function() {
- function click(func) {
- if(obj.hasOwnProperty('id')) {
- document.getElementById(this.id).onclick = func;
- }
- if(obj.hasOwnProperty('name')) {
- var name = document.getElementsByName(this.name);
- for(n in name) {
- name[n].onclick = func;
- }
- }
- if(obj.hasOwnProperty('tagName')) {
- var tags = document.getElementsByTagName(this.tagName);
- for(t in tags) {
- tags[t].onclick = func;
- }
- }
- }
- function bind(obj, func) {
- return function() {
- return func.apply(obj, arguments);
- };
- }
- function $(key) {
- switch(key.charAt(0)) {
- case '#' :
- obj.key = key.replace('#', '');
- break;
- case "." :
- obj.name = key.replace('\\.', '');
- break;
- default :
- obj.tagName = key;
- }
- obj.click = click; // 这两种方式一样
- //obj.click = bind(obj, click);
- return obj;
- }
- // 使用
- $('.name').click(function() {
- alert(111);
- });
- };
- </script>
- <div name="name">CLICK</div>
- //该片段来自于http://www.codesnippet.cn/detail/0311201410871.html
来源: http://www.codesnippet.cn/detail/0311201410871.html