简体   繁体   中英

convert javascript event handler to jquery

I would like to convert the event handler to a jquery style click event but it doesnt seem to like passing the event through, perhaps its because its not an anonymous function anymore?

        // 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);

Your e is undefined inside

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

Change it to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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