我想将事件处理程序转换为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);
答案 0 :(得分:5)
您的e
未定义
$("#faqOne").click(function(){
showFaqOne.showClickedFaq(e);
});
将其更改为
$("#faqOne").click(function(e){//Now e is there
showFaqOne.showClickedFaq(e);
});