【问题标题】:*[id^= Selects all, I'd like to select each same number*[id^= 选择所有,我想选择每个相同的数字
【发布时间】:2011-11-09 07:42:54
【问题描述】:

使用此代码,当您将鼠标悬停在任何带有 id="trigger*" 的东西上时,它会显示所有带有 id="panel*" 的东西,我希望它让 trigger1 显示 panel1,让 trigger2 显示 panel2。

这可能吗?这是我的代码:

$(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 = true;
}).mouseout(function(){
hovered = false;
$('*[id^=trigger]').trigger("mouseout");
});

});

【问题讨论】:

    标签: jquery hover rollover persist selectall


    【解决方案1】:

    如果我了解您的要求,您希望显示离触发的触发器最近的面板。为此使用.closest()

    改变这个:

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

    到这里:

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

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

    【讨论】:

      【解决方案2】:

      使用正则表达式,或者javascript split function将id分成两部分,这样就可以得到id

      例如在我使用的代码中

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

      【讨论】:

        【解决方案3】:

        从触发器对象中提取 id 并使用该 id 来查找面板对象。

        $('*[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;
          }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-03-19
          • 1970-01-01
          • 2013-11-04
          • 1970-01-01
          • 2020-10-05
          • 2018-03-23
          • 1970-01-01
          相关资源
          最近更新 更多