Jquery:变量ID选择器不起作用

Geo*_*ith 3 jquery global-variables jquery-selectors

好的,这是我的代码:

$tabularID = 0;
$('a.swfselector').live('mouseover', function(event) {
            $tabularID= $(this).parent().attr('id');
            $(this).parent().children().not(this).find('.tab').each(function() {
                      $(this).fadeTo(100, 0.4)
            })
            $(this).find('.tab').each(function() {
                      $(this).fadeTo(100,1)
            })

    });
$('#' + $tabularID).live('mouseleave', function(event) {
            alert($tabularID);
            $(this).children().find('.tab').each(function() {
                      $(this).fadeTo(100,1)
            })

    });
Run Code Online (Sandbox Code Playgroud)

Jquery不喜欢这个选择器:

$('#' + $tabularID)
Run Code Online (Sandbox Code Playgroud)

虽然如果我改为:

$('#27')
Run Code Online (Sandbox Code Playgroud)

它警告我的变量$ tabularID就好了,所以我知道它不是错误的变量($ tabularID的输出是27).我需要一个变量,因为父ID会根据鼠标悬停而改变.

任何人都可以看到我不能做的事情?可能真的很明显.

YNh*_*hat 7

您的ID必须以字母az或AZ开头.

此代码$('#'+ $ tabularID)仅在您第一次运行时受到影响.这意味着你的$ tabularID = 0.

当鼠标悬停在它上面时,只更新$ tabularID值,但它不会更新绑定到此对象的事件$('#'+ $ tabularID)

我想你可以改变你的代码:

$tabularID = 0;
$('a.swfselector').live('mouseover', function(event) {
            $tabularID= $(this).parent().attr('id');
            $(this).parent().children().not(this).find('.tab').each(function() {
                      $(this).fadeTo(100, 0.4)
            })
            $(this).find('.tab').each(function() {
                      $(this).fadeTo(100,1)
            })

            $('#' + $tabularID).live('mouseleave', function(event) {
                alert($tabularID);
                $(this).children().find('.tab').each(function() {
                      $(this).fadeTo(100,1)
                })

            });

    });
Run Code Online (Sandbox Code Playgroud)