javascript 点击获取id事件

比如说我点击某个div之后,怎么能获取这个div的id呢
2025-02-22 13:21:27
推荐回答(5个)
回答1:




    
    
    
        div{
            width:50px;
            height:50px;
            margin-left:10px;
            float: left;
            border:1px solid pink;
        }
    
    
        window.onload = function(){
            document.onclick = function(e){
                var target = e.target|| e.srcElement;
                alert(target.id)
            }
        }
    



    this is div 1


    div2


    this is div 3


    this is div 4


    this is div 5


回答2:

onclick="get(this.id)"
这样,在get函数里就可以获得了

回答3:


fsdasa


fsdasa

回答4:

展开

如上,运行了试试吧。

回答5:

document.getElementById("xx");XX就是该DIV的ID

相关问答
最新问答