随机获取链接的属性

window.onload = function(){
                
var links = document.getElementById('hrefs');
                
var attrs = [];
                
var as = links.getElementsByTagName('a');
                
for (var i = 0, len = as.length; i < len; i++) {
                    as[i].onclick 
= function(){
                        
var lTitle = this.getAttribute('title');
                        
var lName = this.getAttribute('name');
                        
var lDefault = this.getAttribute('default');
                        attrs.push(lTitle, lName, lDefault);
                        ClickEvent(attrs);
                        
//return false;
                    }
                }
            }
            
            
function ClickEvent(attrs){
                lens 
= attrs.length;
                
for (var j = 0; j < lens; j++) {
                    document.getElementById(
'print').innerHTML += attrs[j] + '<br />';
                }
            }
<div id="hrefs">
            
<href="javascript:void(0)" title="I am title1" name="I am name1" default="I am default">sohu</a><br>
            
<href="javascript:void(0)" title="I am title2" default="I am default">sohu22</a><br>
            
<href="javascript:void(0)" title="I am title3" name="I am name3">sohu33</a><br>
            
<href="javascript:void(0)" title="I am title4" name="I am name4" default="I am default">sohu44</a>
        
</div>
        
<div id="print"></div>

 

 

 

posted @ 2009-12-07 18:43  豪情  阅读(253)  评论(0编辑  收藏  举报