<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="init.js"></script> <!-- --> <script type="text/javascript" src="index.js"></script> <!-- c - --> </head> <style type="text/css"> div { margin:30px; padding:10px; background: #ccc; border:solid 1px #666; font-family:arial; font-size:12px; } </style> <body> <div class="b-block init_block1"> <!-- --> , ))) </div> <div class="init_block2"> <!-- --> ..))) </div> </body> </html>
/** * , * ( , ) * @param object elem - , * @param string evType - (: "click","mouseover") * @param function call - - ( ) */ function addEvent(elem,evType,call) { if(elem.addEventListener) { elem.addEventListener(evType, call, false); } else if(elem.attachEvent) { elem.attachEvent('on' + evType, call); } } /** * , */ function initStart() { var arrayElem = document.getElementsByTagName('*'); var arrayElemLength = arrayElem.length; for(var i=0;i<arrayElemLength;i++) { var attr = arrayElem[i].className; if(attr) { if(attr.indexOf('init_') !== -1) { var initText = attr.substr(attr.indexOf('init_')); if (initObject[initText]) { initObject[initText](arrayElem[i]) } } } } } /* , */ addEvent(window,'load',initStart);
/** * , */ var initObject = { /** * html-, class "init_block1" * @param object elem - html- ( -) */ init_block1: function(elem) { elem.innerHTML += ' ... '; }, /** * html-, class "init_block2" * @param object elem - html- ( -) */ init_block2: function(elem) { elem.innerHTML += ' . '; addEvent(elem,'click',function(){ alert('!'); }); } }
Source: https://habr.com/ru/post/139990/