当用户将鼠标悬停在列表项上时,将光标置为手

时间:2010-06-21 19:45:45

标签: css

我有一个列表,我的项目有一个点击处理程序:

<ul>
  <li>foo</li>
  <li>goo</li>
</ul>

如何将鼠标指针更改为手形指针(就像将鼠标悬停在按钮上时)?现在,当我将鼠标悬停在列表项上时,指针将变为文本选择指针。

25 个答案:

答案 0 :(得分:2965)

随着时间的推移,正如人们所提到的,你现在可以安全地使用:

li { cursor: pointer; }

答案 1 :(得分:241)

用于li

li:hover {
    cursor: pointer;
}

运行代码段选项后,通过示例查看更多游标属性:

An animation showing a cursor hovering over a div of each class

.auto          { cursor: auto; }
.default       { cursor: default; }
.none          { cursor: none; }
.context-menu  { cursor: context-menu; }
.help          { cursor: help; }
.pointer       { cursor: pointer; }
.progress      { cursor: progress; }
.wait          { cursor: wait; }
.cell          { cursor: cell; }
.crosshair     { cursor: crosshair; }
.text          { cursor: text; }
.vertical-text { cursor: vertical-text; }
.alias         { cursor: alias; }
.copy          { cursor: copy; }
.move          { cursor: move; }
.no-drop       { cursor: no-drop; }
.not-allowed   { cursor: not-allowed; }
.all-scroll    { cursor: all-scroll; }
.col-resize    { cursor: col-resize; }
.row-resize    { cursor: row-resize; }
.n-resize      { cursor: n-resize; }
.e-resize      { cursor: e-resize; }
.s-resize      { cursor: s-resize; }
.w-resize      { cursor: w-resize; }
.ns-resize     { cursor: ns-resize; }
.ew-resize     { cursor: ew-resize; }
.ne-resize     { cursor: ne-resize; }
.nw-resize     { cursor: nw-resize; }
.se-resize     { cursor: se-resize; }
.sw-resize     { cursor: sw-resize; }
.nesw-resize   { cursor: nesw-resize; }
.nwse-resize   { cursor: nwse-resize; }

.cursors > div {
    float: left;
    box-sizing: border-box;
    background: #f2f2f2;
    border:1px solid #ccc;
    width: 20%;
    padding: 10px 2px;
    text-align: center;
    white-space: nowrap;
    &:nth-child(even) {
       background: #eee;
    }
    &:hover {
       opacity: 0.25
    }
}
<h1>Example of cursor</h1>

<div class="cursors">
    <div class="auto">auto</div>
    <div class="default">default</div>
    <div class="none">none</div>
    <div class="context-menu">context-menu</div>
    <div class="help">help</div>
    <div class="pointer">pointer</div>
    <div class="progress">progress</div>
    <div class="wait">wait</div>
    <div class="cell">cell</div>
    <div class="crosshair">crosshair</div>
    <div class="text">text</div>
    <div class="vertical-text">vertical-text</div>
    <div class="alias">alias</div>
    <div class="copy">copy</div>
    <div class="move">move</div>
    <div class="no-drop">no-drop</div>
    <div class="not-allowed">not-allowed</div>
    <div class="all-scroll">all-scroll</div>
    <div class="col-resize">col-resize</div>
    <div class="row-resize">row-resize</div>
    <div class="n-resize">n-resize</div>
    <div class="s-resize">s-resize</div>
    <div class="e-resize">e-resize</div>
    <div class="w-resize">w-resize</div>
    <div class="ns-resize">ns-resize</div>
    <div class="ew-resize">ew-resize</div>
    <div class="ne-resize">ne-resize</div>
    <div class="nw-resize">nw-resize</div>
    <div class="se-resize">se-resize</div>
    <div class="sw-resize">sw-resize</div>
    <div class="nesw-resize">nesw-resize</div>
    <div class="nwse-resize">nwse-resize</div>
</div>

答案 2 :(得分:145)

您不需要jQuery,只需使用以下CSS内容:

li {cursor: pointer}

瞧!方便。

答案 3 :(得分:74)

使用:

li:hover {
    cursor: pointer;
}

可以查看当前HTML规范的其他有效值(hand here

答案 4 :(得分:41)

使用

cursor: pointer;
cursor: hand;

如果你想要一个crossbrowser结果!

答案 5 :(得分:39)

CSS:

.auto            { cursor: auto; }
.default         { cursor: default; }
.none            { cursor: none; }
.context-menu    { cursor: context-menu; }
.help            { cursor: help; }
.pointer         { cursor: pointer; }
.progress        { cursor: progress; }
.wait            { cursor: wait; }
.cell            { cursor: cell; }
.crosshair       { cursor: crosshair; }
.text            { cursor: text; }
.vertical-text   { cursor: vertical-text; }
.alias           { cursor: alias; }
.copy            { cursor: copy; }
.move            { cursor: move; }
.no-drop         { cursor: no-drop; }
.not-allowed     { cursor: not-allowed; }
.all-scroll      { cursor: all-scroll; }
.col-resize      { cursor: col-resize; }
.row-resize      { cursor: row-resize; }
.n-resize        { cursor: n-resize; }
.e-resize        { cursor: e-resize; }
.s-resize        { cursor: s-resize; }
.w-resize        { cursor: w-resize; }
.ns-resize       { cursor: ns-resize; }
.ew-resize       { cursor: ew-resize; }
.ne-resize       { cursor: ne-resize; }
.nw-resize       { cursor: nw-resize; }
.se-resize       { cursor: se-resize; }
.sw-resize       { cursor: sw-resize; }
.nesw-resize     { cursor: nesw-resize; }
.nwse-resize     { cursor: nwse-resize; }

您也可以将光标变为图像:

.img-cur {
   cursor: url(images/cursor.png), auto;
}

答案 6 :(得分:16)

li:hover {cursor: hand; cursor: pointer;}

答案 7 :(得分:16)

我认为只有在JavaScript可用时才显示手/指针光标是明智的。所以人们不会觉得他们可以点击那些不可点击的东西。

要实现这一点,您可以使用JavaScript将CSS添加到元素中,如此

$("li").css({"cursor":"pointer"});

或直接将其链接到点击处理程序。

或者当modernizer<html class="no-js">结合使用时,CSS将如下所示:

.js li { cursor: pointer; }

答案 8 :(得分:15)

对于完整的跨浏览器,请使用:

cursor: pointer;
cursor: hand;

答案 9 :(得分:14)

为了完整性:

cursor: -webkit-grab;

它还提供了一只手,即你在移动图像视图时所知道的那种。

如果你想使用jQuery和mousedown emulate grab behavior,这非常有用。

Enter image description here

答案 10 :(得分:9)

只需做这样的事情:

li { 
  cursor: pointer;
}

我将它应用于您的代码以查看其工作原理:

&#13;
&#13;
li {
  cursor: pointer;
}
&#13;
<ul>
  <li>foo</li>
  <li>goo</li>
</ul>
&#13;
&#13;
&#13;

注意:同样不要忘记你可以使用自定义光标的任何手形光标,你可以像这样创建一个fav手形图标:

&#13;
&#13;
div {
  display: block;
  width: 400px;
  height: 400px;
  background: red;
  cursor: url(http://findicons.com/files/icons/1840/free_style/128/hand.png) 4 12, auto;
}
&#13;
<div>
</div>
&#13;
&#13;
&#13;

答案 11 :(得分:8)

为了能够使任何东西得到“鼠标改变”处理,你可以添加一个CSS类:

.mousechange:hover {
  cursor: pointer;
}
<span class="mousechange">Some text here</span>

我不会说使用cursor:hand,因为它仅适用于Internet Explorer 5.5及更低版本,而Internet Explorer 6则附带Windows XP(2002)。当浏览器停止为他们工作时,人们只会得到提升的提示。此外,在Visual Studio中,它将红色下划线表示该条目。它告诉我:

  

验证(CSS 3.0):“hand”不是“游标”的有效值   属性

答案 12 :(得分:8)

ul li:hover{
   cursor: pointer;
}

答案 13 :(得分:6)

使用:

ul li:hover{
   cursor: pointer;
}

有关更多鼠标事件,请检查CSS cursor property

答案 14 :(得分:6)

所有其他响应建议使用标准CSS指针,但是,有两种方法:

  1. 将CSS属性cursor:pointer;应用于元素。 (这是光标悬停在按钮上时的默认样式。)

  2. 使用指针的自定义图形应用CSS属性cursor:url(pointer.png);。如果您想确保所有平台上的用户体验相同(而不是允许浏览器/操作系统决定您的指针光标应该是什么样子),这可能是更理想的。请注意,如果找不到图片,可能会添加后备选项,包括辅助网址或任何其他选项,例如cursor:url(pointer.png,fallback.png,pointer);

  3. 当然,这些可以以这种方式应用于列表项li{cursor:pointer;},作为类.class{cursor:pointer;},或者作为每个元素style="cursor:pointer;"的样式属性的值。

答案 15 :(得分:4)

您可以使用以下其中一项:

li:hover
{
 cursor: pointer;
}

li
{
 cursor: pointer;
}

工作示例1:

    li:hover
    {
     cursor: pointer;
    }
<ul>
  <li>foo</li>
  <li>bar</li>
</ul>

工作示例2:

    li
    {
     cursor: pointer;
    }
<ul>
  <li>foo</li>
  <li>bar</li>
</ul>

答案 16 :(得分:2)

使用HTML Hack

注意:不建议这样做,因为它被视为不良做法

将内容包含在包含href属性的锚标记中,无需明确应用cursor: pointer;属性以及锚属性的副作用(使用CSS修改):

<a href="#" style="text-decoration: initial; color: initial;"><div>This is bad practice, but it works.</div></a>

答案 17 :(得分:2)

对于基本的手形符号:

尝试

cursor: pointer 

如果您想要一个手形符号,例如拖动某个项目并放下它,请尝试:

cursor: grab

答案 18 :(得分:2)

您还可以使用以下样式:

li {
    cursor: grabbing;
}

答案 19 :(得分:2)

您可以使用以下代码:

dftest=pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns= ['w','v1','v2','v3']) df['x']=np.random.choice(a=[False, True], size=(1, 10), p=[0.5, 0.5])[0]

答案 20 :(得分:1)

检查以下内容。我是从W3Schools获得的。

<!DOCTYPE html>
<html>
    <head>
        <style>
        .alias {cursor: alias;}
        .all-scroll {cursor: all-scroll;}
        .auto {cursor: auto;}
        .cell {cursor: cell;}
        .context-menu {cursor: context-menu;}
        .col-resize {cursor: col-resize;}
        .copy {cursor: copy;}
        .crosshair {cursor: crosshair;}
        .default {cursor: default;}
        .e-resize {cursor: e-resize;}
        .ew-resize {cursor: ew-resize;}
        .grab {cursor: -webkit-grab; cursor: grab;}
        .grabbing {cursor: -webkit-grabbing; cursor: grabbing;}
        .help {cursor: help;}
        .move {cursor: move;}
        .n-resize {cursor: n-resize;}
        .ne-resize {cursor: ne-resize;}
        .nesw-resize {cursor: nesw-resize;}
        .ns-resize {cursor: ns-resize;}
        .nw-resize {cursor: nw-resize;}
        .nwse-resize {cursor: nwse-resize;}
        .no-drop {cursor: no-drop;}
        .none {cursor: none;}
        .not-allowed {cursor: not-allowed;}
        .pointer {cursor: pointer;}
        .progress {cursor: progress;}
        .row-resize {cursor: row-resize;}
        .s-resize {cursor: s-resize;}
        .se-resize {cursor: se-resize;}
        .sw-resize {cursor: sw-resize;}
        .text {cursor: text;}
        .url {cursor: url(myBall.cur),auto;}
        .w-resize {cursor: w-resize;}
        .wait {cursor: wait;}
        .zoom-in {cursor: zoom-in;}
        .zoom-out {cursor: zoom-out;}
        </style>
    </head>

    <body>
        <h1>The cursor property</h1>
        <p>Mouse over the words to change the mouse cursor.</p>

        <p class="alias">alias</p>
        <p class="all-scroll">all-scroll</p>
        <p class="auto">auto</p>
        <p class="cell">cell</p>
        <p class="context-menu">context-menu</p>
        <p class="col-resize">col-resize</p>
        <p class="copy">copy</p>
        <p class="crosshair">crosshair</p>
        <p class="default">default</p>
        <p class="e-resize">e-resize</p>
        <p class="ew-resize">ew-resize</p>
        <p class="grab">grab</p>
        <p class="grabbing">grabbing</p>
        <p class="help">help</p>
        <p class="move">move</p>
        <p class="n-resize">n-resize</p>
        <p class="ne-resize">ne-resize</p>
        <p class="nesw-resize">nesw-resize</p>
        <p class="ns-resize">ns-resize</p>
        <p class="nw-resize">nw-resize</p>
        <p class="nwse-resize">nwse-resize</p>
        <p class="no-drop">no-drop</p>
        <p class="none">none</p>
        <p class="not-allowed">not-allowed</p>
        <p class="pointer">pointer</p>
        <p class="progress">progress</p>
        <p class="row-resize">row-resize</p>
        <p class="s-resize">s-resize</p>
        <p class="se-resize">se-resize</p>
        <p class="sw-resize">sw-resize</p>
        <p class="text">text</p>
        <p class="url">url</p>
        <p class="w-resize">w-resize</p>
        <p class="wait">wait</p>
        <p class="zoom-in">zoom-in</p>
        <p class="zoom-out">zoom-out</p>
    </body>
</html>

答案 21 :(得分:1)

<style>
.para{
    color: black;
}
.para:hover{
    cursor: pointer;
    color: blue;
}
</style>
<div class="para">

在上面的HTML代码中,[:hover]用于指示以下样式仅在悬停或将鼠标光标停留在其上时才适用。

CSS中有几种游标类型:

查看以下代码以了解游标类型:

<style>
.alias {cursor: alias;}
.all-scroll {cursor: all-scroll;}
.auto {cursor: auto;}
.cell {cursor: cell;}
.context-menu {cursor: context-menu;}
.col-resize {cursor: col-resize;}
.copy {cursor: copy;}
.crosshair {cursor: crosshair;}
.default {cursor: default;}
.e-resize {cursor: e-resize;}
.ew-resize {cursor: ew-resize;}
.grab {cursor: -webkit-grab; cursor: grab;}
.grabbing {cursor: -webkit-grabbing; cursor: grabbing;}
.help {cursor: help;}
.move {cursor: move;}
.n-resize {cursor: n-resize;}
.ne-resize {cursor: ne-resize;}
.nesw-resize {cursor: nesw-resize;}
.ns-resize {cursor: ns-resize;}
.nw-resize {cursor: nw-resize;}
.nwse-resize {cursor: nwse-resize;}
.no-drop {cursor: no-drop;}
.none {cursor: none;}
.not-allowed {cursor: not-allowed;}
.pointer {cursor: pointer;}
.progress {cursor: progress;}
.row-resize {cursor: row-resize;}
.s-resize {cursor: s-resize;}
.se-resize {cursor: se-resize;}
.sw-resize {cursor: sw-resize;}
.text {cursor: text;}
.url {cursor: url(myBall.cur),auto;}
.w-resize {cursor: w-resize;}
.wait {cursor: wait;}
.zoom-in {cursor: zoom-in;}
.zoom-out {cursor: zoom-out;}
</style>

单击下面的链接以查看cursor属性的行为:

this stackoverflow question

答案 22 :(得分:0)

仅使用CSS设置自定义光标指针

https://developer.mozilla.org/en-US/docs/Web/CSS/cursor


/* Keyword value */
cursor: pointer;
cursor: auto;

/* URL, with a keyword fallback */
cursor: url(hand.cur), pointer;

/* URL and coordinates, with a keyword fallback */
cursor: url(cursor1.png) 4 12, auto;
cursor: url(cursor2.png) 2 2, pointer;

/* Global values */
cursor: inherit;
cursor: initial;
cursor: unset;

  

演示

<ul>
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ul>

  

hand.cur图片

li:hover{
   cursor: url("../icons/hand.cur"), pointer;
}

答案 23 :(得分:0)

您可以在悬停时更改它,也可以仅在列表项上指定 cursor:pointer,两者都可以。

ul li {
  cursor: pointer;
}

替代

ul li:hover {
  cursor: pointer;
}

答案 24 :(得分:-3)

这应该有效:

<style>
    li:hover {
        cursor: hand;
    }
</style>