English 中文(简体)
在选择元素类更改后重新重新初始化 jQuery 函数吗?
原标题:Reinitialize jQuery function after selector elements class changes?

我有两个 JQuery 函数, 能为相同的元素提供不同的功能 。

以下是我基本的 HTML 标记 :

<ul>
  <li class="active">
    <a href="#head1" data-toggle="tab" class="fade">
      <img src="head1_down.jpg" />
    </a>
  </li>
  <li>
    <a href="#head2" data-toggle="tab" class="fade">
      <img src="head2_down.jpg" />
    </a>
  </li>
  <li>
    <a href="#head3" data-toggle="tab">
      <img src="head2_down.jpg" />
      <!-- Does not fade! No .fade class on link tag. -->
    </a>
  </li>
</ul>

第一个函数 LookFade () 添加一个淡化效果, 改变 mousehover 上的图像源 :

// Helper functions
$.fn.lookUp = function() {
  $(this).attr( src , function(i,val) { return val.replace( down.jpg ,  up.jpg ) });
  return $(this);
}

$.fn.lookDown = function() {
  $(this).attr( src , function(i,val) { return val.replace( up.jpg ,  down.jpg ) });
  return $(this);
}

$.fn.lookFade = function() {
  $(this).hover(
    function() {
      $(this).fadeOut(function() {
        $(this).lookUp().fadeIn();
      })
    },
    function() {
      $(this).fadeOut(function() {
        $(this).lookDown().fadeIn();
      })
    }
  );
  return $(this);
}

// Change .active image on pageload
$( li.active > a.fade > img ).lookUp();

// Fade effect on hover
$( li:not(.active) > a.fade > img ).lookFade();

第二个函数在单击链接项时切换内容窗格( 未在标记中显示, 此工作! ) 。 它还会更改链接标签内的图像, 并更改 < code>. active 类, 从当前 li 元素到单击 li 元素 。

// Toggle tabs and change .active
$( a[data-toggle="tab"] ).click(function() {
  var head = $(this).attr( href );
  var active = "#" + $( .pane.active ).attr( id );
  if (head != active) {
    $(active).removeClass( active );
    $(head).addClass( active );
    $(this).children( img ).lookUp();
    $(this).parent().parent().children( li.active ).removeClass( active );
    $(this).parent().addClass( active );
  }
});

problem: 当点击一个链接, 它使内容窗面发生改变, 甚至班级也得到正确调整。 但 LookFade () 函数不承认类的变化, 但仍会为现在的. active li 元素淡化 。

最佳回答

更改元素的类别不会改变它所约束的事件。 事件是受元素约束的, 而不是该类。 如果您想要该类型的功能, 请使用 < a href=" http:// api.jquery.com/ on/ " rel=" nofollow". on () 或 < a href=" http:// api.jquery.com/ delgate/" rel=" nofolp".delegate ()

由于您正在使用插件来执行这些动作, 使其生效并不容易。 我将放弃 < code> LookFade 方法, 并使用事件授权约束 < code> LookFade 事件 ( mouseenter museleave ) 。

快速示例 :

$( ul ).delegate( li:not(.active) > a.fade > img , mouseenter ,function(){
    $(this).fadeOut(function() {
        $(this).lookUp().fadeIn();
    });
});
问题回答

暂无回答




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签