简体   繁体   English

如何使用jquery检测事件绑定到元素

[英]how to detect an event bind to an element with jquery

this is what i want to do: 这就是我想做的:

I have many anchors in a webpage, some are managed with ajax since they are binded to a click event with jquery; 我的网页中有很多锚点,有些锚点是用ajax管理的,因为它们是通过jquery绑定到click事件的; some are not so they are sending to another page via regular http request. 有些不是,所以他们通过常规的http请求发送到另一个页面。

<a href="#/something" >something</a>
<a href="#/ajax/something" class="blabla">ajax something</a>

<script type="text/javascript">
    $(function(){
       $('.blabla').click(function(){
          //doing some ajax return false so anchor don't redirect
          return false;
       });
    });
</script>

Please don't respond regard to anchor clases, like 'hey select all anchors that don't have "blabla" class'. 请不要回应有关锚类的问题,例如“嘿,请选择所有没有“ blabla”类的锚”。 I'm doing this because i'm triying to put a "waiting" label over the page for every anchor that is not been managed via ajax. 我这样做是因为我试图为未通过Ajax管理的每个锚在页面上放置“等待”标签。 I want to know if an anchor has a click event binded to itself. 我想知道锚点是否具有绑定到其自身的click事件。

Regards. 问候。

This works - the idea is: 这行得通-这个想法是:

  • Store how many click events exist for each <a> element in an array when the page initially loads. 最初加载页面时,存储数组中每个<a>元素存在多少click事件。

  • Then, every 500ms or so, compare the length of click events on each anchor to the length you stored in the array 然后,每隔500毫秒左右,将每个锚点上的click事件的长度与您存储在数组中的长度进行比较

- -

$(function() {
  i = 0;
  window.ahrefEvents = [];
  $('a').each(function() { 
    $(this).attr('data-id', i); //give each anchor a unique id and save it the data-id attribute. we'll then save the number of events for each link in window.ahrefEvents
    window.ahrefEvents[i] = $._data($(this)[0], 'events') == undefined ? 0 : $._data($(this)[0], 'events')['click'].length; //if no events are bound it'll return undefined, so check for that and set it to 0 if that's the case
    i++;
});


window.setInterval(function() { //check every 500ms to see if that length changed
    checkEvents();
}, 500);

window.checkEvents = function() {
    $('a').each(function() {
        index = $(this).attr('data-id');
        if ($._data($(this)[0], 'events') != undefined) {
            if (window.ahrefEvents[index] != $._data($(this)[0], 'events')['click'].length) {
                //fires when length of click events on any anchor has changed
                console.log('event bound on anchor #' + index);
            }
        }
    });
  }
});​

Demo: http://fiddle.jshell.net/CyYbA/20/show/ 演示: http : //fiddle.jshell.net/CyYbA/20/show/

Try pulling up console and binding something to the $('#test') object 尝试拉起控制台并将某些东西绑定到$('#test')对象

edit fiddle here: http://jsfiddle.net/CyYbA/20/ 在这里编辑小提琴: http : //jsfiddle.net/CyYbA/20/

也许您可以使用jquery 数据功能存储这些锚点的其他信息?

If your url system for AJAX always includes ajax in the path you can build a selector based on href 如果您的AJAX网址系统始终在路径中包含ajax ,则可以基于href建立选择器

var $noAjaxLinks= $('a').filter(function(){

      return ! /ajax/.test(this.href);
})

ok, this is what i have so far: 好的,这是我到目前为止的内容:

<ul class="nav nav-list">
    <li><a href="content.php" class="load">content via .load()</a></li>
    <li><a href="content.php" class="get">content via $.get()</a></li>
    <li><a href="content.php" class="post">content via $.post()</a></li>
    <li><a href="content.php" class="ajax">content via $.ajax()</a></li>
    <li><a href="content.php" class="">content 2 without ajax()</a></li>
</ul>

some jquery events binding 一些jQuery事件绑定

$('a.load').click(function(){
    $("#container").loader().load($(this).attr('href'));
    return false;
});
$('a.get').click(function(){
    $.get($(this).attr('href'), function(d){
        $('#container').html(d);
    });
    return false;
});

i have many anchors, one without ajax and the else ones with a different ajax call so that's what i'm expecting to find in the page. 我有很多锚,一个没有ajax,另一个带有不同的ajax调用,所以这就是我希望在页面中找到的。 my code have to catch all of them and evaluate which one has not an ajax call to treat it diferent 我的代码必须捕获所有这些代码,并评估哪个代码没有ajax调用,以区别对待

here's the code. 这是代码。

//looking just for one anchor
$('a').each(function(idx, obj){
    var events = $._data(this,'events');
    if (typeof events != "undefined" && "click" in events) {
        for (i in events.click) {
            if (typeof events.click[i].handler != "undefined") {
                 var f = events.click[i].handler.toString();
                 if ((/\$(\(.*\))?\.(load|ajax|get|post)\(/).exec(f)) {
                    console.log('obj '+$(this).text()+' : with ajax.');
                    //this is where i'm gonna deal with this regular http anchor...
                 }
            }
        }
    }
});

hope this help another guy with the same problem i had. 希望这可以帮助另一个遇到相同问题的人。 regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM