javascript为网页元素绑定click事件

 1 <fieldset>

 2   <legend>Web API</legend>

 3   <table>

 4     <tr><th>Method</th><td>get</td></tr>

 5     <tr><th>Return</th><td>null</td></tr>

 6   </table>

 7 </fieldset>

 8 

 9 <script language="javaScript">

10   var show = function(event){

11   obj = this.nextElementSibling? this.nextElementSibling: event.srcElement.nextSibling;

12   var agent = navigator.userAgent.toLowerCase();

13   var display = !/opera/.test(agent) && /msie/.test(agent)? "block": "table";

14   if(obj.style.display==display){

15     obj.style.display = "none";

16     obj.parentNode.style.width = "18%";

17   }

18   else{

19     obj.style.display = display;

20     obj.parentNode.style.width = "98%";

21   }

22 

23 };

24 

25 function addClickEvent(){

26   var elems = document.getElementsByTagName("legend");

27   for(var i=0; i<elems.length; i++){

28     elems.addEventListener? elems.addEventListener( "click", show, false ):

29     elems.attachEvent("onclick", show);

30   }

31 }

32 addClickEvent();

33 </script>

 

你可能感兴趣的:(JavaScript)