日期:2014-05-20  浏览次数:20502 次

js给<a>标记加事件
如题

------解决方案--------------------
现查找出所有的a标记,然后添加
如:$("#divId").find("a").bind("click",function(){alert('123');})

------解决方案--------------------
HTML code
<body>
<a href="#">A</a>
<a href="#">B</a>
<a href="#">C</a>
<a href="#">D</a>
    <script type="text/javascript">
        var item = document.getElementsByTagName("a");
        for(var i=0;i<item.length;i++){
            item[i].onclick=function(){
                alert(this.innerHTML);
            }
        }
    </script>
</body>

------解决方案--------------------
楼上的几个,我曰!人家没说用jquery。原始的js如下:

添加事件:
function addEvent( obj, type, fn ) {
if ( obj.attachEvent ) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
} else
obj.addEventListener( type, fn, false );
}
删除事件:
function removeEvent( obj, type, fn ) {
if ( obj.detachEvent ) {
obj.detachEvent( 'on'+type, obj[type+fn] );
obj[type+fn] = null;
} else
obj.removeEventListener( type, fn, false );
}


更详细的介绍:
http://www.360doc.com/content/10/1013/17/1485725_60705922.shtml