简体   繁体   English

点击事件多次触发(应该是一次)

[英]click event fired more than once ( supposed to be once)

this is my first post, here I wanted to create a chrome extension, when it is pressed, it will show links which can be pressed, links retrieved using AJAX. 这是我的第一篇文章,在这里我想创建一个chrome扩展名,当按下它时,它将显示可以按下的链接,以及使用AJAX检索的链接。 It worked so far, the only problem is when it is clicked on any link, sometimes it appeared twice or even thrice. 到目前为止,它仍然有效,唯一的问题是单击任何链接时,有时它出现两次甚至三次。 can anyone give an explanation and answer to this problem? 谁能给这个问题一个解释和答案?

Thank you! 谢谢!

var links = [];
links.push("http://www.tenmanga.com/book/rss/id-16896.xml");
links.push("http://mangafox.me/rss/fairy_tail.xml");
links.push("http://feeds.feedburner.com/9gag?format=xml");

Function Start() 函数Start()

function start(){
    for (var i in links)
        getXML(links[i]);
    $(document).ready(function(){
    });


}

Function getXML 函数getXML

function getXML(url){
    var test = $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        success: function(xml){
            parse(xml);
bind();
        }
    });
    return test;
}

function bind(){
    $("a").click(function(e){
        chrome.tabs.create({url:$(this).attr("href")});
    });
}
function parse(xml){
    //$("#content").append();
    var title = $(xml).find('title').first().text();
    var createClickHandler = function(arg){
        return function(){ 
            open_item(arg); 
        };
    }
    $("#content").append(title+"<br>");
    $(xml).find('item').each(function(){
        var temp = document.createElement("a");
        var title = this.childNodes[1].textContent;
        var link = this.childNodes[3].textContent;
        temp.innerHTML = title;
        temp.setAttribute("href",link);
        $("#content").append(temp);
        $("#content").append("<br>");
    });
    $("#content").append("<br>");
}

START HERE! 从这里开始!

var start = start();

The only reason to invoke click event more than once when clicked on an elements is, more than one event handler is registered over that. 在元素上单击时多次调用click事件的唯一原因是,在该事件上注册了多个事件处理程序。 Each time you call bind what u have done is binding to event to 'a' so i guess for the first link it event is fired once for the seconds twice and for the third thrice.May be instead of using just $('a') that attributes to all the 'a' element in the page you may dynamically make 'a' with separate ids. 每次您调用bind时,您所做的都是将事件绑定到'a',所以我想对于第一个链接,事件被触发两次,两次触发两次,第三次触发。可能不是使用$('a' )该属性归于页面中的所有“ a”元素,您可以使用单独的ID动态创建“ a”。 That must end the weird behavior. 那必须结束奇怪的行为。

Invoke e.preventDefault(); 调用e.preventDefault(); where e is event in your click callback so that event won't bubble up and won't trigger anything else. 这里eevent在点击的回调,这样的事件不会冒泡,并且不会触发任何东西。

Also bind() should not be called on each success after ajax returned. 而且,ajax返回后,每次成功都不应调用bind()。

instead of binding on each link, do the following once: 而不是绑定每个链接,请执行一次以下操作:

$(document).on('click','a'
    function(e){
        chrome.tabs.create({url:$(this).attr("href")});
        return false;
    }
);

if you have no other anchors on link 如果链接上没有其他锚点

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

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