如何使用jquery检查哪些元素点击绑定到它们?

时间:2022-03-02 14:29:56

I want to chck which elements in my page are clickable, meaning that they either have href attribute (which is easy to check), or they have click event binded to them via JS . is there any way to do so ?

我想知道我的页面中的哪些元素是可点击的,这意味着它们要么具有href属性(易于检查),要么通过JS将它们绑定到它们。有没有办法这样做?

2 个解决方案

#1


2  

using the solution provided for this question: How to debug JavaScript/jQuery event bindings with Firebug (or similar tool)

使用为此问题提供的解决方案:如何使用Firebug(或类似工具)调试JavaScript / jQuery事件绑定

i think this may be what you need:

我想这可能是你需要的:

<html>
<head>
   <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script
</head>
<body>
    <a href="test">test link</a>
    <span>test span</span>
<script type="text/javascript">
$.fn.listHandlers = function(events, outputFunction) {
    return this.each(function(i){
        var elem = this,
            dEvents = $(this).data('events');
        if (!dEvents) {return;}
        $.each(dEvents, function(name, handler){
            if((new RegExp('^(' + (events === '*' ? '.+' : events.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
               $.each(handler, function(i,handler){
                   outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
               });
           }
        });
    });
};


$("span").click(function(){ alert("test");});

var clickableObjects = [];
$(document).ready(function(){
    $('*').listHandlers('click',function(element,data){
        clickableObjects.push(element);
    }).filter("a").each(function(index, value){
        clickableObjects.push(value);
    });

});


</script>
</body>
</html>

#2


0  

Why not assign a .clickable class to elements with an href attribute and ensure you also assign a .clickable class to items when you .bind() them?

为什么不将.clickable类分配给具有href属性的元素,并确保在.bind()它们时也为项目分配.clickable类?

#1


2  

using the solution provided for this question: How to debug JavaScript/jQuery event bindings with Firebug (or similar tool)

使用为此问题提供的解决方案:如何使用Firebug(或类似工具)调试JavaScript / jQuery事件绑定

i think this may be what you need:

我想这可能是你需要的:

<html>
<head>
   <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script
</head>
<body>
    <a href="test">test link</a>
    <span>test span</span>
<script type="text/javascript">
$.fn.listHandlers = function(events, outputFunction) {
    return this.each(function(i){
        var elem = this,
            dEvents = $(this).data('events');
        if (!dEvents) {return;}
        $.each(dEvents, function(name, handler){
            if((new RegExp('^(' + (events === '*' ? '.+' : events.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
               $.each(handler, function(i,handler){
                   outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
               });
           }
        });
    });
};


$("span").click(function(){ alert("test");});

var clickableObjects = [];
$(document).ready(function(){
    $('*').listHandlers('click',function(element,data){
        clickableObjects.push(element);
    }).filter("a").each(function(index, value){
        clickableObjects.push(value);
    });

});


</script>
</body>
</html>

#2


0  

Why not assign a .clickable class to elements with an href attribute and ensure you also assign a .clickable class to items when you .bind() them?

为什么不将.clickable类分配给具有href属性的元素,并确保在.bind()它们时也为项目分配.clickable类?