簡體   English   中英

如何將多個事件應用於同一個函數

[英]How to apply multiple events to the same 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");
});

});

什么是e ,活動? 您需要將click()參數命名為click()函數以使用它。 另外,要調用show_text以使其具有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);
});

你也有一個額外的)對你的最終}); 線。

您可以使用jQuery bind將多個事件附加到單個函數。

$('#whatever').bind('mouseover focus click', function() {
   your_custom_function();
});

你在找這樣的東西嗎?

var handle = function(event) {
  $(event.currentTarget).show_text(event);
};
$info_items.bind('click blur', handle);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM