鼠标悬停在另一个函数创建的元素上时运行的javascript事件

时间:2014-06-25 23:35:31

标签: javascript jquery raphael

此函数create_canvas_card()创建一个包含较小框的框。只要鼠标位于其中一个框上,如何让它调用函数card_mouseover()?

 function create_canvas_card(card_data, each_card){//where card_data is an element/object and each_card is an int
  click_canvas_card_x = 10, click_canvas_card_y = 10;//these are set elsewhere
  image_id = $(card_data.node).data('card')
  click_canvas_cards[each_card] = click_canvas.rect(click_canvas_card_x, click_canvas_card_y, 40, 40).attr('fill', 'url(/images/thumbnails/image'+ image_id +'.jpg)');

//my attempt      
$(card_data.node).bind('mouseover', function(e){
    var card = cards[$(this).data('card')];
    card_mouseover(card);
  });

//another attempt
//click_canvas_cards[each_card].mouseover(click_canvas_card_mouseover(card_data.node));
}

从此循环中调用

      for(each_card in cards_to_create_for_click){
        var card_data = cards_to_create_for_click[each_card];
        create_canvas_card(card_data, each_card);
      }

到目前为止,两者都没有效果。

1 个答案:

答案 0 :(得分:1)

使用Raphael内置的Element.hover()方法。

Element.hover

我已经做了一个快速的小提示,向你展示它here

对于你的例子,我想你想把你的功能改成这样的东西:

 function create_canvas_card(card_data, each_card){
     click_canvas_card_x = 10, click_canvas_card_y = 10;//these are set elsewhere
     image_id = $(card_data.node).data('card')
     click_canvas_cards[each_card] = click_canvas.rect(click_canvas_card_x,      click_canvas_card_y, 40, 40)
     .attr('fill', 'url(/images/thumbnails/image'+ image_id +'.jpg)')
     .hover(function(){
         //Do something on hover
     });