【问题标题】:Mouse events on any underlying div (without selecting class/id)任何底层 div 上的鼠标事件(不选择类/id)
【发布时间】:2014-04-25 19:54:36
【问题描述】:

我有一个带有id=1 的透明 div,我需要将鼠标事件传递给所有底层 div(不管类/id),同时保持这个透明 div 对其自己的鼠标事件有反应。

因此,按类/id 选择底层 div,pointer-events: none / display: none 不是选项。

$('#1').mouseenter(function() {
    $(this).css('background','rgba(0, 120, 255, 0.3)');
});
$('#a').mouseenter(function() {
   $(this).css('background', 'red');
});
$('#1').mouseleave(function() {
    $(this).css('background','rgba(0, 120, 255, 0.2)');
});
$('#a').mouseleave(function() {
   $(this).css('background', '#555555');
});

http://jsfiddle.net/B6SAy/

如何以独立于类/id 的方式实现这一点?

如果这意味着什么,我同时只有 1 个底层 div,但该 div 可以有任何类/id。

【问题讨论】:

    标签: jquery jquery-selectors mouseevent


    【解决方案1】:

    您可以尝试 mouseover()、mouseout() 和/或 hover(),而不是 mouseenter() 和 mouseleave()。这些链接可能会有所帮助:

    StackOverflow: Jquery mouseenter() vs mouseover()

    Ben Nadel: jQuery Events: MouseOver / MouseOut vs. MouseEnter / MouseLeave

    您还可以在 api.jquery.com 查看每个函数的文档。

    【讨论】:

    • 对低级 div 有什么帮助?
    • 您可以通过更好地解释添加函数文档的链接来改进您的答案。
    • 好的,我添加了链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多