jquery: test whether input variable is dom element

前端 未结 4 1930
清歌不尽
清歌不尽 2021-01-02 03:50

I would like to write a jquery function that accepts either a dom element or its id as input:

function myfunction(myinput){
 // pseudocode:
 // if (myinput i         


        
4条回答
  •  醉酒成梦
    2021-01-02 04:02

    You would implement your function like this:

    function myfunction(myinput){
    
     if (myinput.nodeType){
        var myID = $(myinput).attr('id');
     } else {
        var myID = myinput;
     }
    
     // Do stuff with myID ...
    

    }

    More information about nodeType.

提交回复
热议问题