开发者

JavaScript: distinguish DOM element objects and Event objects [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Javascript isDOM — How do you check if a Javascript Object is a DOM Object?

I have the following simple function:

function do_smth(el){

    if(typeof el != 'object'){
        el = this
    }
    var val = $.trim(el.value)

     /**** some code here ****/

}

Sometimes it is binded to an element as an event

1)

element.onclick = do_smth

and sometimes it is used the following way

2开发者_开发知识库)

do_smth(element)

both ways this should work good...

The problem is that I get the el as Event object in the first case, even if there are no arguments passed. So typeof el != 'object' does not work as expected.

How can I distinguish DOM element or Event?


function do_smth(el){
    el = el.nodeType == 1 ? el : this;
    var val = $.trim(el.value)
}


To distingusih a DOM element do

if(el.nodeType)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜