好的,这是我的代码:
$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/