【发布时间】:2011-01-18 06:05:04
【问题描述】:
在 jquery 中将鼠标悬停在 td 上时,如何使 td 中的 URL 触发“a:hover”css 效果
【问题讨论】:
标签: jquery hover html-table
在 jquery 中将鼠标悬停在 td 上时,如何使 td 中的 URL 触发“a:hover”css 效果
【问题讨论】:
标签: jquery hover html-table
你可以试试:
$('td').hover(function() {
$(this).find('a').mouseover();
}, function() {
$(this).find('a').mouseout();
});
【讨论】:
$('td').live('hover',function() { $(this).find('a.desc').mouseover(); }, function() { $(this).find('a .desc').mouseout(); });
我希望它这么简单,似乎那行不通,因为我正在通过 ajax 加载表格,所以即使添加 live 也无济于事......
$('td').live('hover',function() { $(this).find('a.desc').mouseover(); }, 功能() { $(this).find('a.desc').mouseout(); });【讨论】:
$('td').hover(function() {
$(this).find('a').addClass('name-class-simulates-hover');
}, function() {
$(this).find('a').removeClass('name-class-simulates-hover');
});
也许添加一个与你的 a:hover 效果相同的类?
【讨论】:
某些事件无法通过 .live 或其他方式处理,因此,如果您发现自己需要将事件添加到新内容中,在通过 ajax 添加到页面之后或实际上作为添加到页面的一部分,您可以通过以下方式添加事件管理调用函数。
function AddHover()
{
$('td').hover(function() {
$(this).find('a').addClass('myHoverClass');
}, function() {
$(this).find('a').removeClass('myHoverClass');
});
};
并在 ajax 加载中:
剪辑...
success: function(msg)
{
LoadTableData(msg);// this function would process and add your page elements
AddHover();// this then adds the events to the new markup
},
...结束片段 - 有关“成功:”的更多详细信息,请参阅 jQuery .ajax:
【讨论】:
感谢您的意见,Thomas 和 Mark。
Thomas:我尝试了你的方法,但没有奏效,但它给了我一个想法,可以稍微调整一下,我得到了它的工作......
$('tr').live('mouseenter',function() {
$(this).find('a.desc').addClass('bold');
}).live('mouseleave',function(){ $(this).find('a.desc').removeClass('bold'); });
上面的代码是我当前的解决方案,它的工作,悬停似乎不起作用,因为再次需要 live 来删除“bold”类,因此我使用 mouseenter 和 mouseleave。
在 $(this).find('a.desc').mouseover(); 上使用鼠标悬停这是不可能的...它甚至让谷歌浏览器滞后与众不同...所以我确信这不是一个好主意...
Mark:这是一个非常有趣的想法,我会试一试,我尽量避免使用“.live()”,这似乎不是一个有效的方法,但是对于 web 2.0 来说这很难,
【讨论】:
突出显示代码吗?