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

标签 jquery jquery-selectors global-variables

好的,这是我的代码:

$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)
            })

    });

Jquery 不喜欢这个选择器:

$('#' + $tabularID)

虽然如果我将其更改为:

$('#27')

它会很好地提醒我的变量 $tabularID,所以我知道这不是错误的变量($tabularID 的输出是 27)。我在这里需要一个变量,因为父 ID 会根据鼠标悬停的位置而变化。

有人能看到我看不到的东西吗?可能真的很明显。

最佳答案

您的 ID 必须以字母 a-z 或 A-Z 开头。

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

当您将鼠标悬停在其上时,只会更新 $tabularID 值,但不会更新该对象 $('#' + $tabularID) 事件的绑定(bind)

我认为你可以像这样更改你的代码:

$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)
                })

            });

    });

关于Jquery:变量作为 ID 选择器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5787746/

相关文章:

jquery - 使用jquery设置td的值

jquery - 在 Ajax 成功事件中启用复选框吗?

javascript - Jquery $(function(){}),是什么意思?

javascript - 无法在函数中读取关联数组/对象

javascript - 单击不同的按钮时如何更改 div 内容.javascript php html

jquery - Firefox 中的 javaScript 错误 - 无法使用事件类更新菜单项

jquery - 页脚和奇怪的间距和定位

javascript - 在空 jQuery 选择器数组上返回 "True"?

c - 在 C 项目中共享 "Char * "

Javascript 全局变量正在重置