【问题标题】:Add hover event ( cursor pointer property) to body but not to its children将悬停事件(光标指针属性)添加到正文而不是其子项
【发布时间】:2015-03-24 21:42:25
【问题描述】:

代码示例:

<body>
    <div id="something" style="width: 50vw; height: 300px;">
        </div>
    <div class="else">oekdoekod</div>
</body>

问题是我只需要在悬停主体时使用光标指针效果,而不是用id="something" 悬停div

没用

:not(#something) {
    cursor: pointer;
}

但是

$(document.body).on("hover", ":not(#something, #something *)",function(e){ 
   $(this).addClass("pointer"); 
     e.stopPropagation(); 
});

用css

.pointer {
    cursor: pointer;
}

它有效,但仅适用于其他元素,例如.else

如何将pointer 光标应用到hover 事件上,仅在正文上而不在#something 上?

【问题讨论】:

  • 在你的 CSS *:not(#something) { cursor:pointer; 之前使用 * }

标签: jquery css hover


【解决方案1】:

这对我有用,你在什么浏览器中测试它?这是JSBin

CSS

body{
  cursor:crosshair;

}

#something {
  width: 50vw;
  height: 300px;
  background-color:rgba(0,0,255,.2);
  cursor:pointer;
}

#else{
  width: 50vw;
  height: 300px;
  background-color:rgba(0,255,255,.2);
}

html

<div id="something"></div>
<div id="else"></div>

【讨论】:

  • 它工作!太容易了,谢谢!
【解决方案2】:

根据您的描述,在正文而不是 #something 上有一个指针光标,这应该可以解决问题:

body {
    cursor: pointer;
}
#something {
    cursor: auto;
}

【讨论】:

    猜你喜欢
    • 2012-12-15
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多