开发者

jquery select any dom element on page

开发者 https://www.devze.com 2023-01-27 08:35 出处:网络
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.

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

精彩评论

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