如何将多个事件应用于同一个函数

Tri*_*rip 3 javascript oop jquery function

我不是这个jquery的最佳人选.但我试图从函数中分离动作,以便我可以应用多个导致相同功能的事件.不幸的是,这不起作用.谁知道为什么?

更新了功能,但仍然是错误

$(document).ready(function() {

var $info_items = jQuery('.checkbox.has_info, .has_info');

$info_items.click(function(event) {
$(this).show_text(event);
});


// I suspect it has something to do with this initalizer of the function here 

jQuery.fn.show_text = function(event){
  var $info_item = jQuery(this);
  $info_items.filter(function(index){
    return $(".hidden_text").css("display","block");
    }).not($info_item).parent().next().next().hide("slow");
  $info_item.parent().next().next().show("fast");
});

});
Run Code Online (Sandbox Code Playgroud)

mea*_*gar 7

什么是e事件?您需要将click()函数的event参数命名为使用它.此外,要调用show_text它具有a this,您需要在元素上调用它:

$info_items.click(function (event) {
  // 'this' is the element in $info_items which was clicked

  // invoke show_text on the element in question
  $(this).show_text(event);
});
Run Code Online (Sandbox Code Playgroud)

)最后});一行还有一个额外的.