将javascript事件处理程序转换为jquery

时间:2015-04-28 08:36:35

标签: javascript jquery events handler anonymous-function

我想将事件处理程序转换为jquery样式的click事件,但它似乎不喜欢传递事件,也许是因为它不再是匿名函数了?

        // variables
        var faqOne = document.getElementById("faqOne");
        var $hiddenOne = $(".faqOneHidden");


        // javascript event handler works!
        faqOne.addEventListener("click", function(e){
        showFaqOne.showClickedFaq(e);
        }, false);
        // javascript event handle - doesnt work!
        $("#faqOne").click(function(){
            showFaqOne.showClickedFaq(e);
        });
        // constructor
        function DisplayQFaqs(link, faq){
            this.link = link;
            this.faq = faq;
        }
        // method prototype
        DisplayQFaqs.prototype.showClickedFaq = function(e){
                var el = e.currentTarget;
                if(el === this.link) {

                   this.faq.toggle("slow", function(){
                   });
                }
        };
        // new DisplayQFaqs Objects
        var showFaqOne = new DisplayQFaqs(faqOne,$hiddenOne);

1 个答案:

答案 0 :(得分:5)

您的e未定义

$("#faqOne").click(function(){
        showFaqOne.showClickedFaq(e);
    });

将其更改为

   $("#faqOne").click(function(e){//Now e is there
        showFaqOne.showClickedFaq(e);
    });