开发者

*[id^= Selects all, I'd like to select each same number

With this code, when you hover over anything with the id="trigger*", it shows everything with the id="panel*" I would like it, for trigger1 to show panel1, for trigger2 to show panel2.

Is that possible? This is my code:

$(document).ready(function(){
hovered = false;

$('*[id^=trigger]').bind('mouseenter mouseleave', function(event) {
switch(event.type) {
    case 'mouseenter':
       // when user enters the div

       $('*[id^=panel]').show('fast');

    break;
    case 'mouseleave':
      // leaves
         setTimeout(function(){
  if(!hovered) {
      $('*[id^=panel]').hide('fast');
      }}, 250);
    break;
}
});

$('*[id^=panel]').mouseover(function(){
hovered = t开发者_Go百科rue;
}).mouseout(function(){
hovered = false;
$('*[id^=trigger]').trigger("mouseout");
});

});


If I'm understanding your requirements, you would like to show the closest panel to the trigger that was triggered. Use .closest() for this.

Change this:

$('*[id^=panel]').show('fast');

To this:

$(this).closest('*[id^=panel]').show('fast');

http://api.jquery.com/closest/


use a regular expression, or javascript split function to split the id into two parts and that way obtain the id

like for example in my code i use

var draggable_id = $(this).attr('id').split('button')[0];


extract the id from the trigger object and use that id to find the panel object.

$('*[id^=trigger]').bind('mouseenter mouseleave', function(event) {
  // extract id from trigger object
  var id = this.id.substring(7);
  switch(event.type) {
  case 'mouseenter':
    // when user enters the div
    $('*[id^=panel'+id+']').show();
  break;
  case 'mouseleave':
    $('*[id^=panel'+id+']').hide();
  break;
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜