How to do “If Clicked Else ..”

后端 未结 8 1644
灰色年华
灰色年华 2021-01-31 05:48

I am trying to use jQuery to do something like

if(jQuery(\'#id\').click) {
    //do-some-stuff
} else {
    //run function2
}

But I\'m unsure h

8条回答
  •  没有蜡笔的小新
    2021-01-31 06:23

    Maybe you're looking for something like this:

    $(document).click(function(e)
       {
           if($(e.srcElement).attr('id')=='id')
           {
               alert('click on #id');
           }
           else
           {
                alert('click on something else'); 
           }
       });
    

    jsfiddle

    You may retrieve a pointer to the clicked element using event.srcElement .

    So all you have to do is to check the id-attribute of the clicked element.

提交回复
热议问题