weixin_33737134 2010-05-09 09:38 采纳率: 0%
浏览 19

jQuery类选择器奇怪

I'm using jQuery to change the background image of a button depending on the class associated with it on hover. It only works if I put the hover statements in separate functions, however. Why is this?

Here's the NON working code, always evaluates to the .submit hover statement, even when that class is removed via the keyup event.

    $(function() {
    {

$('.submit-getinfo').hover(function ()
{
    $(this).css( {backgroundPosition: "right bottom"} );
}, function() {
    $(this).css( {backgroundPosition: "right top"} );
    //$(this).removeClass('submithover');

});

$('.submit').hover(function ()
{
    $(this).css( {backgroundPosition: "left bottom"} );
}, function() {
    $(this).css( {backgroundPosition: "left top"} );
    //$(this).removeClass('submithover');

});

    }});

Working code:

    $(function() {
    {


$('.submit').hover(function ()
{
    $(this).css( {backgroundPosition: "left bottom"} );
}, function() {
    $(this).css( {backgroundPosition: "left top"} );
    //$(this).removeClass('submithover');

});
    }});

    $('#test').bind('keyup', function() { 

    if (url == 'devel') {
    $("#submit").addClass("submit-getinfo").removeClass("submit");

$('.submit-getinfo').hover(function ()
{
    $(this).css( {backgroundPosition: "right bottom"} );
}, function() {
    $(this).css( {backgroundPosition: "right top"} );
//$(this).removeClass('submithover');

});
    } } );

I just fail to see why I have to put the hover statements in separate functions, instead of sticking both in the main DOM.

  • 写回答

1条回答 默认 最新

  • 旧行李 2010-05-09 10:39
    关注

    You need to re-arrange your code a bit using .live(), like this:

    $(function() {
      $('.submit-getinfo').live('mouseenter', function () {
          $(this).css( {backgroundPosition: "right bottom"} );
      }).live('mouseleave' function() {
          $(this).css( {backgroundPosition: "right top"} );
      });
    
      $('.submit').live('mouseenter', function () {
          $(this).css( {backgroundPosition: "left bottom"} );
      }).live('mouseleave', function() {
          $(this).css( {backgroundPosition: "left top"} );
      });
    });
    

    Currently it's saying "find elements with that class, bind this hover function to those", now it doesn't matter that you're changing the class, because the function's already bound to that element's mouseenter and mouseleave jQuery events...even if it's class changes, which is unrelated.

    With .live() however, the event handler isn't bound to the element itself, it's bound to the document, waiting for the mouseenter and mouseleave events to bubble up, it evaluates if the element causing the events matches the selector right now, and executes if that's the case...so if you change the class it will matter, make sense?

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog