开发者

jquery select any dom element on page

Does anyone know how to select any element (possibly on click) on page like body is selected, div is selected, div#foo etc... so i can place it to some variable and edit it later on.

i've tried

$("*", document.body).click(function (e) {  
 e.stopPropagation开发者_如何学运维();  
 var domEl = $(this).get(0);  
 alert("Clicked on - " + domEl.tagName);  
});

but it's not working for all elements


You want to get the target property of the event:

$(document).click(function(e) {
   e.stopPropagation();
   var domEl = e.target; // or $(e.target) to jQuerytize it
   alert("Clicked on - " + domEl.tagName);
});

See: http://www.quirksmode.org/js/events_properties.html

Demo: http://jsfiddle.net/karim79/dyHEA/


Try this.

$(function(){
    $("*").click(function () {
         console.log($(this));
         return false;
        }
    ); 
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜