如何在$(document).on()&之间打开,关闭jquery中的$(“a”)。on()

时间:2017-02-28 07:02:30

标签: javascript jquery dom onclicklistener

我正在实施扩展程序以记录所有浏览器操作 $(document).on()正在少数几个网站上工作,$("a").on()正在少数网站上工作。所以我把它们放在dom.js的单个文件中。截至目前,它正在录制两个动作。如何处理只记录一个动作?

请帮帮我。

var bindEventToNewElement = function(e) {

    var target = $(e.target.tagName);

    $(target).off().on('click',function (e) {
        alert("target");
         if (e.target !== this) return;
        // e.stopPropagation();

        var content="";
        if($(this).text()!==undefined){

            content=$(this).text();

        }

        if(($(this).attr('href')=="")||($(this).attr('href')=="#")||($(this).attr('href')==null)) {

            sendmsg(getPath(this), "click",'',content,"link",traverseElement(this),window.location.href,getElementName(this));

        }else{

            sendmsg($(this).attr('href'), "redirect", '',content,"link",traverseElement(this),window.location.href,getElementName(this));

        }

        $(target).off();
      
        $(document).off('click','a');
        $(document).on('click','a');
        $("a").on('click');
    });

};

/**
 * DOMNodeInserted event for adding dynamic html content
 */
$(document).on('DOMNodeInserted','a',bindEventToNewElement);


/*

 * Event of clicking on anchor tag and attaching event to root level document

 */
$(document).on('click','a', function(obj) {

    if (obj.target !== this) return;
   // obj.stopPropagation();
    
    var content="";
    if($(this).text()!==undefined){

        content=$(this).text();

    }

    if(($(this).attr('href')=="")||($(this).attr('href')=="#")||($(this).attr('href')==null)) {

        sendmsg(getPath(this), "click",'',content,"link",traverseElement(this),window.location.href,getElementName(this));

    }else{

        sendmsg($(this).attr('href'), "redirect", '',content,"link",traverseElement(this),window.location.href,getElementName(this));

    }


});

/*

 * Event of clicking on anchor tag and attaching event handler directly to the DOM

 */
$("a").on('click',function(obj) {

    if (obj.target !== this) return;

    var content="";
    if($(this).text()!==undefined){

        content=$(this).text();

    }

    if(($(this).attr('href')=="")||($(this).attr('href')=="#")||($(this).attr('href')==null)) {

        sendmsg(getPath(this), "click",'',content,"link",traverseElement(this),window.location.href,getElementName(this));

    }else{

        sendmsg($(this).attr('href'), "redirect", '',content,"link",traverseElement(this),window.location.href,getElementName(this));

    }

     $(document).off('click','a');

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

0 个答案:

没有答案